numerical-methods

Dealing with float precision in Javascript [duplicate]

泪湿孤枕 提交于 2019-11-25 23:49:22
问题 This question already has an answer here: How to deal with floating point number precision in JavaScript? 40 answers I have a large amount of numeric values y in javascript. I want to group them by rounding them down to the nearest multiple of x and convert the result to a string. How do I get around the annoying floating point precision? For example: 0.2 + 0.4 = 0.6000000000000001 Two things I have tried: >>> y = 1.23456789 >>> x = 0.2 >>> parseInt(Math.round(Math.floor(y/x))) * x; 1