我正在显示这个变量,
var coords = 'Co-ords(' + hero.x + "," + hero.y + ')';
然而,当它显示时,它出现像294.99999999999
这样的大量数字我怎样才能将其四舍五入到两位小数?
答案 0 :(得分:3)
使用toFixed(n)
舍入到特定的小数位数。
浮点数由于它们表示数字的方式而不准确。如果你舍入到一定数量的小数位,那么准确性通常不会成为问题。
var coords = 'Co-ords(' + hero.x.toFixed(2) + "," + hero.y.toFixed(2) + ')';