I have the following dummy test script:
function test() {
var x = 0.1 * 0.2;
document.write(x);
}
test();
This will print the result 0.020000000000000004 while it should just print 0.02 (if you use your calculator). As far as I understood this is due to errors in the floating point multiplication precision.
Does anyone have a good solution so that in such case I get the correct result 0.02? I know there are functions like toFixed or rounding would be another possibility, but I'd like to really have the whole number printed without any cutting and rounding. Just wanted to know if one of you has some nice, elegant solution.
Of course, otherwise I'll round to some 10 digits or so.
Floating-Point Guide: http://floating-point-gui.de/
BigDecimal for JavaScript: https://github.com/royNiladri/js-big-...
DecimalJS: https://mikemcl.github.io/decimal.js/
Смотрите видео How can I deal with floating point number precision in JavaScript? онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Code Samples 13 Апрель 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 144 раз и оно понравилось 0 людям.