有没有一种很好的方法来显示一直显示小数点后一位但不四舍五入的十进制数字?所以你有以下数字:
3.55
3.5
3
我想让他们展示
3.5
3.5
3.0
您也可以使用该toFixed()方法,但这会将浮点数转换为字符串。
var float = 3.55
float.toFixed(1);
这将四舍五入,但在任何位置舍入小数点 0.5
改为使用Math.floor。
pad(Math.floor(num * 10) / 10);
如果需要,在哪里pad添加。.0
请记住,这Math.round可能会更好。既然3.55应该3.6。
试试这个(其中 x 是你的浮点数):
function format_float(float x )
 {
   if ((x).tostring().contains(".") ) 
   {return parseFloat((x).tostring().Substring(0,indexof(".")+1)) ;}
   else 
   {return x.toFixed(1);}
 }
要消除对 pad 功能的需要:
function toOnePlace(n) {
  n = ('' + n).split('.');
  return n[0] + '.' + (n[1]? n[1].charAt(0) : 0);
}