dpd20130 2017-07-10 09:51
浏览 18

算术方程式在PHP / JavaScript中查询

I'm not too much of an expert in Maths but when we try to equate 19.95 / 2.85 in any calculator we get the output as 7.

Trying the same arithmetic equation in PHP:

$val = 19.95 / 2.85;
echo $val; // 7
echo floor($val); // 6

Trying the same arithmetic equation in JavaScript:

var val = 19.95 / 2.85;
console.log(val); // 6.999999999999999
console.log(Math.floor(val)); // 6

How can I make sure that when i use floor() in PHP that the output I get from the above arithmetic equation equals 7?

  • 写回答

1条回答 默认 最新

  • dprq18175 2017-07-10 10:42
    关注

    You want to get near a value of number ex:

    6.5 the near natural integer of is 7

    6.4 the near natural integer of is 6

    You can to that with javascript with Math.round

    var val = 19.95 / 2.85;
    console.log(val); // 6.999999999999999
    console.log(Math.round(val)); // 7
    
    评论

报告相同问题?