[removed] Comparing two float values

前端 未结 4 1805
忘了有多久
忘了有多久 2020-12-09 01:55

I have this JavaScript function:

Contrl.prototype.EvaluateStatement = function(acVal, cfVal) {

    var cv = parseFloat(cfVal).toFixed(2);
    var av = parse         


        
4条回答
  •  被撕碎了的回忆
    2020-12-09 02:33

    Comparing floats using short notation, also accepts floats as strings and integers:

    var floatOne = 2, floatTwo = '1.456';
    
    Math.floor(floatOne*100) > Math.floor(floatTwo*100) 
    

    (!) Note: Comparison happens using integers. What actually happens behind the scenes: 200 > 145

    Extend 100 with zero's for more decimal precision. For example use 1000 for 3 decimals precision.

    Test:

    var floatOne = 2, floatTwo = '1.456';
    console.log(Math.floor(floatOne*100), '>', Math.floor(floatTwo*100), '=', Math.floor(floatOne*100) > Math.floor(floatTwo*100));

提交回复
热议问题