Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.4 KiB

  1. /***
  2. *_fptostr.c - workhorse routine for converting floating point to string
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Workhorse routine for fcvt, ecvt.
  8. *
  9. *Revision History:
  10. * 09-17-84 DFW created
  11. * 03-05-90 GJF Fixed calling type, added #include <cruntime.h>,
  12. * removed #include <register.h>, fixed copyright. Also,
  13. * cleaned up the formatting a bit.
  14. * 07-20-90 SBM Compiles cleanly with -W3 (added #include <string.h>)
  15. * 08-01-90 SBM Renamed <struct.h> to <fltintrn.h>
  16. * 09-27-90 GJF New-style function declarator.
  17. * 06-11-92 GDP Bug fix: Shorten string if leadig (overflow) digit is 1
  18. * 10-09-92 GDP Backed out last fix for ecvt (printf regressed)
  19. * 04-06-93 SKS Replace _CRTAPI* with _cdecl
  20. * 04-27-94 CFW Replace modified GDP 06-11-92 fix.
  21. * 08-05-94 JWM Backed out CFW's fix for ecvt (printf regression!).
  22. *
  23. *******************************************************************************/
  24. #include <cruntime.h>
  25. #include <string.h>
  26. #include <fltintrn.h>
  27. /***
  28. *void _fptostr(buf, digits, pflt) - workhorse floating point conversion
  29. *
  30. *Purpose:
  31. * This is the workhorse routine for fcvt, ecvt. Here is where
  32. * all the digits are put into a buffer and the rounding is
  33. * performed and indicators of the decimal point position are set. Note,
  34. * this must not change the mantissa field of pflt since routines which
  35. * use this routine rely on this being unchanged.
  36. *
  37. *Entry:
  38. * char *buf - the buffer in which the digits are to be put
  39. * int digits - the number of digits which are to go into the buffer
  40. * STRFLT pflt - a pointer to a structure containing information on the
  41. * floating point value, including a string containing the
  42. * non-zero significant digits of the mantissa.
  43. *
  44. *Exit:
  45. * Changes the contents of the buffer and also may increment the decpt
  46. * field of the structure pointer to by the 'pflt' parameter if overflow
  47. * occurs during rounding (e.g. 9.999999... gets rounded to 10.000...).
  48. *
  49. *Exceptions:
  50. *
  51. *******************************************************************************/
  52. void __cdecl _fptostr (
  53. char *buf,
  54. REG4 int digits,
  55. REG3 STRFLT pflt
  56. )
  57. {
  58. REG1 char *pbuf = buf;
  59. REG2 char *mantissa = pflt->mantissa;
  60. /* initialize the first digit in the buffer to '0' (NOTE - NOT '\0')
  61. * and set the pointer to the second digit of the buffer. The first
  62. * digit is used to handle overflow on rounding (e.g. 9.9999...
  63. * becomes 10.000...) which requires a carry into the first digit.
  64. */
  65. *pbuf++ = '0';
  66. /* Copy the digits of the value into the buffer (with 0 padding)
  67. * and insert the terminating null character.
  68. */
  69. while (digits > 0) {
  70. *pbuf++ = (*mantissa) ? *mantissa++ : (char)'0';
  71. digits--;
  72. }
  73. *pbuf = '\0';
  74. /* do any rounding which may be needed. Note - if digits < 0 don't
  75. * do any rounding since in this case, the rounding occurs in a digit
  76. * which will not be output beause of the precision requested
  77. */
  78. if (digits >= 0 && *mantissa >= '5') {
  79. pbuf--;
  80. while (*pbuf == '9')
  81. *pbuf-- = '0';
  82. *pbuf += 1;
  83. }
  84. if (*buf == '1') {
  85. /* the rounding caused overflow into the leading digit (e.g.
  86. * 9.999.. went to 10.000...), so increment the decpt position
  87. * by 1
  88. */
  89. pflt->decpt++;
  90. }
  91. else {
  92. /* move the entire string to the left one digit to remove the
  93. * unused overflow digit.
  94. */
  95. memmove(buf, buf+1, strlen(buf+1)+1);
  96. }
  97. }