chakra-core / ChakraCore

ChakraCore is an open source Javascript engine with a C API.
MIT License
9.12k stars 1.2k forks source link

Intl NumberFormat bug for floating point number rounding behavior #3691

Closed vijaykumar51 closed 7 years ago

vijaykumar51 commented 7 years ago

I observed strange behavior while using Intl library for number format purpose. I tried this code on IE 11 with document mode Edge. Code mentioned below produces output 40.42 but it should be 40.43

Intl.NumberFormat("ja-JP",{style: "decimal", currency: "JPY", minimumFractionDigits: 2, maximumFractionDigits: 2}).format(40.425)

I observed the same erroneous output for floating numbers from - 32.425 to 40.425, keeping the decimal fraction .425 or .925. Below are some cases that I ran.

Cases with wrong output

Input => Output 32.425 => 32.42 37.425 => 37.42 32.925 => 32.92 37.925 => 37.92

Cases with correct output

Input => Output 32.625 => 32.63 37.257 => 37.26 45.425 => 45.43 12.925 => 12.93

If I change the style from decimal to currency then output for 40.425 comes out to be 40.43 which is correct. But for decimal style output is 40.42.

dilijev commented 7 years ago

Note: IE 11 Document Mode Edge just means "latest in IE 11" and is not equivalent to Microsoft Edge in any sense.

That said, this issue does repro in Edge.

The locale and most of the options are unimportant for the repro. I tried with various locales and even in other scripts (tried locale 'ar' for Arabic numerals). Here is a reduced repro:

## Source
print(Intl.NumberFormat(undefined,{maximumFractionDigits: 2}).format(40.425))

┌──────────────────┬───────┐
│ d8               │ 40.43 │
│ jsc              │       │
│ node             │       │
│ sm               │       │
├──────────────────┼───────┤
│ ch-1.7.1         │ 40.42 │
│ ch-master-latest │       │
│ node-ch          │       │
└──────────────────┴───────┘

It's possible this behavior is coming from WinGlob. I'll investigate that.

@MikeHolman do you know anything about rounding behavior in ChakraCore?

dilijev commented 7 years ago

Tracking as part of #3644