Skip to content

Commit 9d0ab13

Browse files
committed
[MERGE #1550 @obastemur] Fix toFixed bug #1511
Merge pull request #1550 from obastemur:tofixed_bug ``` print(1.25499999999999989342.toFixed(2)); print(1.255.toFixed(2)); ``` Code above should print 1.25 while it was printing 1.26 Fixes #1511
2 parents dc74543 + 7fcc80e commit 9d0ab13

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

lib/Common/Common/NumberUtilities_strtod.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,7 +2199,7 @@ static int RoundTo(byte *pbSrc, byte *pbLim, int nDigits, __out_bcount(nDigits+1
21992199
{
22002200
int i = nDigits;
22012201

2202-
if( pbSrc[i] >= 5 )
2202+
if( pbSrc[i] > 5 )
22032203
{
22042204
// Add 1 to the BCD representation.
22052205
for( i = nDigits - 1; i >= 0; i-- )
@@ -2322,7 +2322,7 @@ int Js::NumberUtilities::FDblToStr(double dbl, Js::NumberUtilities::FormatType f
23222322
else
23232323
{
23242324
//Special case: When negative power of 10 is more than most significant digit.
2325-
if( rgb[0] >= 5 )
2325+
if( rgb[0] > 5 )
23262326
{
23272327
rgbAdj[0] = 1;
23282328
wExp10 += 1;

test/Number/toString.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ function runTest(numberToTestAsString)
3939
writeLine("n.toPrecision(5): " + n.toPrecision(5));
4040
writeLine("n.toPrecision(20): " + n.toPrecision(20));
4141

42+
// test toFixed toString round formatting
43+
if ( !(1.25499999999999989342.toFixed(2) + "" == "1.25") ||
44+
!(1.255.toFixed(2) + "" == "1.25") ) {
45+
throw Error("1.255.toFixed(2) != 1.25 ??");
46+
}
4247
writeLine("");
4348
}
4449

0 commit comments

Comments
 (0)