Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenecvt.c
Go to the documentation of this file.
00001 /* 00002 * PROJECT: ReactOS CRT 00003 * LICENSE: See COPYING in the top level directory 00004 * PURPOSE: CRT's ecvt 00005 * FILE: lib/sdk/crt/stdlib/ecvt.c 00006 * PROGRAMERS: Gregor Schneider (parts based on ecvtbuf.c by DJ Delorie) 00007 */ 00008 00009 #include <precomp.h> 00010 #define NUMBER_EFMT 18 /* sign, dot, null, 15 for alignment */ 00011 00012 /* 00013 * @implemented 00014 */ 00015 char * 00016 _ecvt (double value, int ndigits, int *decpt, int *sign) 00017 { 00018 static char ecvtbuf[DBL_MAX_10_EXP + 10]; 00019 char *cvtbuf, *s, *d; 00020 00021 if (ndigits < 0) ndigits = 0; 00022 s = cvtbuf = (char*)malloc(ndigits + NUMBER_EFMT); 00023 d = ecvtbuf; 00024 00025 *sign = 0; 00026 *decpt = 0; 00027 00028 if (cvtbuf == NULL) 00029 { 00030 return NULL; 00031 } 00032 00033 sprintf(cvtbuf, "%-+.*E", ndigits, value); 00034 /* Treat special values */ 00035 if (strncmp(s, "NaN", 3) == 0) 00036 { 00037 memcpy(ecvtbuf, s, 4); 00038 } 00039 else if (strncmp(s + 1, "Inf", 3) == 0) 00040 { 00041 memcpy(ecvtbuf, s, 5); 00042 } 00043 else 00044 { 00045 /* Set the sign */ 00046 if (*s && *s == '-') 00047 { 00048 *sign = 1; 00049 } 00050 s++; 00051 /* Copy the first digit */ 00052 if (*s && *s != '.') 00053 { 00054 if (d - ecvtbuf < ndigits) 00055 { 00056 *d++ = *s++; 00057 } 00058 else 00059 { 00060 s++; 00061 } 00062 } 00063 /* Skip the decimal point */ 00064 if (*s && *s == '.') 00065 { 00066 s++; 00067 } 00068 /* Copy fractional digits */ 00069 while (*s && *s != 'E') 00070 { 00071 if (d - ecvtbuf < ndigits) 00072 { 00073 *d++ = *s++; 00074 } 00075 else 00076 { 00077 s++; 00078 } 00079 } 00080 /* Skip the exponent */ 00081 if (*s && *s == 'E') 00082 { 00083 s++; 00084 } 00085 /* Set the decimal point to the exponent value plus the one digit we copied */ 00086 *decpt = atoi(s) + 1; 00087 /* Handle special decimal point cases */ 00088 if (cvtbuf[1] == '0') 00089 { 00090 *decpt = 0; 00091 } 00092 if (ndigits < 1) 00093 { 00094 /* Need enhanced precision*/ 00095 char* tbuf = (char*)malloc(NUMBER_EFMT); 00096 if (tbuf == NULL) 00097 { 00098 free(cvtbuf); 00099 return NULL; 00100 } 00101 sprintf(tbuf, "%-+.*E", ndigits + 2, value); 00102 if (tbuf[1] >= '5') 00103 { 00104 (*decpt)++; 00105 } 00106 free(tbuf); 00107 } 00108 /* Pad with zeroes */ 00109 while (d - ecvtbuf < ndigits) 00110 { 00111 *d++ = '0'; 00112 } 00113 /* Terminate */ 00114 *d = '\0'; 00115 } 00116 free(cvtbuf); 00117 return ecvtbuf; 00118 } Generated on Sun May 27 2012 04:36:37 for ReactOS by
1.7.6.1
|