简体   繁体   English

十进制比较,不舍入javascript

[英]Decimal Comparison without rounding in javascript

I have 2 textboxex for decimal values and I need to compare them in JavaScript. 我有2个textboxex用于十进制值,我需要在JavaScript中对其进行比较。 The scenario is 该方案是

var first = $('#txtFirst').val();
var second= $('#txtSecond').val();

In textboxex I'm entering following values 在textboxex中,我输入以下值

  • first => 99999999999998.999999997 首先=> 99999999999998.999999997

  • second => 99999999999998.999999991 秒=> 99999999999998.999999991

I tried the below code 我尝试了以下代码

     if (parseFloat(parseFloat(first).toFixed(10)) <= parseFloat(parseFloat(second).toFixed(10))) 

This returns true because it rounds it so both the values becomes 99999999999999. How to fix it? 这将返回true,因为它会四舍五入,因此两个值都变为99999999999999。如何解决?

Just compare without converting into integer 只是比较而不转换为整数

var first = $('#txtFirst').val();
var second= $('#txtSecond').val();

if ( first == second )
{
  // they are equal
}

if you want to compare upto 10 decimals then 如果您要比较最多10位小数,则

var first10Decimals  = first.split(".").pop().substring(0,10);
var second10Decimals  = second.split(".").pop().substring(0,10);
if ( first10Decimals  == second10Decimals )
{
   //they are equal
}

Vanilla JavaScript can't handle such big numbers. Vanilla JavaScript无法处理这么大的数字。 You should use something like big.js that is designed to work with arbitrary large numbers : 您应该使用诸如big.js之类的设计用于任意大数的东西:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM