Skip to content

Commit 8a40e1b

Browse files
committed
Add handling of double values with just integer part
1 parent a7f5b0e commit 8a40e1b

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/CLR/Helpers/nanoprintf/nanoprintf.c

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// Portions Copyright (c) 2006 - 2021 Skirrid Systems. All rights reserved.
44
// See LICENSE file in the project root for full license information.
@@ -93,7 +93,7 @@ Floating point
9393
#define DP_LIMIT 310
9494
#define MAX_POWER 256
9595
// [NF_CHANGE]
96-
#define FLOAT_DIGITS 18
96+
#define FLOAT_DIGITS 19
9797
// [END_NF_CHANGE]
9898
#else
9999
#define DP_LIMIT 40
@@ -147,6 +147,31 @@ static char *trim_zeros(char *p)
147147
return p;
148148
}
149149

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+
150175
/* ---------------------------------------------------------------------------
151176
Function: format_float()
152177
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,
271296
* to the answer in the fastest time, with the minimum number of
272297
* operations to introduce rounding errors.
273298
*/
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.
274314
// First make small numbers bigger.
275315

276316
i = 0;

0 commit comments

Comments
 (0)