|
1 |
| -// |
| 1 | +// |
2 | 2 | // Copyright (c) .NET Foundation and Contributors
|
3 | 3 | // Portions Copyright (c) 2006 - 2021 Skirrid Systems. All rights reserved.
|
4 | 4 | // See LICENSE file in the project root for full license information.
|
@@ -93,7 +93,7 @@ Floating point
|
93 | 93 | #define DP_LIMIT 310
|
94 | 94 | #define MAX_POWER 256
|
95 | 95 | // [NF_CHANGE]
|
96 |
| - #define FLOAT_DIGITS 18 |
| 96 | + #define FLOAT_DIGITS 19 |
97 | 97 | // [END_NF_CHANGE]
|
98 | 98 | #else
|
99 | 99 | #define DP_LIMIT 40
|
@@ -147,6 +147,31 @@ static char *trim_zeros(char *p)
|
147 | 147 | return p;
|
148 | 148 | }
|
149 | 149 |
|
| 150 | +// [NF_CHANGE] |
| 151 | + |
| 152 | +flt_width_t count_integer_digits(double number) { |
| 153 | + // Handle negative numbers |
| 154 | + if (number < 0) { |
| 155 | + number = -number; |
| 156 | + } |
| 157 | + |
| 158 | + // Handle special case for zero |
| 159 | + if (number < 1.0) { |
| 160 | + return 1; |
| 161 | + } |
| 162 | + |
| 163 | + flt_width_t count = 0; |
| 164 | + |
| 165 | + while (number >= 1.0) { |
| 166 | + number /= 10.0; |
| 167 | + count++; |
| 168 | + } |
| 169 | + |
| 170 | + return count; |
| 171 | +} |
| 172 | + |
| 173 | +// [END_NF_CHANGE] |
| 174 | + |
150 | 175 | /* ---------------------------------------------------------------------------
|
151 | 176 | Function: format_float()
|
152 | 177 | Called from the main doprnt function to handle formatting of floating point
|
@@ -271,6 +296,21 @@ static char *format_float(double number, flt_width_t ndigits, flt_width_t width,
|
271 | 296 | * to the answer in the fastest time, with the minimum number of
|
272 | 297 | * operations to introduce rounding errors.
|
273 | 298 | */
|
| 299 | + |
| 300 | + // [NF_CHANGE] |
| 301 | + |
| 302 | + // check if number is integer |
| 303 | + if (number == (int)number) |
| 304 | + { |
| 305 | + // number is an integer |
| 306 | + |
| 307 | + // set number of digits required to have 0 decimal places |
| 308 | + ndigits = count_integer_digits(number); |
| 309 | + } |
| 310 | + |
| 311 | + // [END_NF_CHANGE] |
| 312 | + |
| 313 | + // Normalise the number such that it lies in the range 1 <= n < 10. |
274 | 314 | // First make small numbers bigger.
|
275 | 315 |
|
276 | 316 | i = 0;
|
|
0 commit comments