Check out the description of toFixed on 
MDN: 
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

It doesn't cover your specific example, but it does cover the differences 
between, e.g., Number(2.35).toFixed(1) and Number(2.55).toFixed(1).

The TL;DR here is that floating point numbers are represented in a 
non-intuitive way internally to your computer (IEEE 754), and the toFixed 
function doesn't take the time to perform the rounding in the way you 
expect. You can generally achieve the rounding you want my multiplying your 
number by a factor (in your case, 100), calling Math.round() on the result, 
dividing it again by the same factor used previously, and then formatting 
that with toFixed().

On Thursday, March 24, 2022 at 3:35:43 PM UTC-7 [email protected] wrote:

> By looking into this example:
> var x = 1.245;
> var xFixed = x.toFixed(2);
> xFixed value should be 1.25
> but it is printing 1.24
>
>

-- 
-- 
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- 
You received this message because you are subscribed to the Google Groups 
"v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-dev/661c29d0-e528-47bd-a565-2e447606f073n%40googlegroups.com.

Reply via email to