Javascript浮动比较 [英] Javascript float comparison

查看:232
本文介绍了Javascript浮动比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript中进行数字比较时遇到了大问题.

I'm having a big problem with number comparison in javascript.

该脚本指控比较"7 <10"为假.

The script accuses that the comparison "7 < 10" is false.

console.clear();

var min = parseFloat("5").toFixed(2);
var max = parseFloat("10").toFixed(2);
var value = parseFloat("7").toFixed(2);

console.log(min, max, value);

console.log(value > min); // OK.
console.log(value < max); // ---- false ??????

有人知道什么在玩吗?

推荐答案

事实证明.toFixed()返回字符串-在比较值以查看结果之前尝试添加parseFloat:

As it turns out .toFixed() returns strings - Try adding parseFloat before comparing the values to see the result:

console.log(parseFloat(value) < parseFloat(max)); // ---- now true

这篇关于Javascript浮动比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆