關於微信小程序中的取整

關於微信小程序中的取整

1.四捨五入函數——toFixed()

var a=1.1;
var b=1.6;
console.log(a.toFixed());
console.log(b.toFixed());

保留兩位小數——toFixed(2)

var c = 1.34535345;
console.log(c.toFixed(2));

2.Math函數

向下取整 Math.floor(num);
Math.floor(1.1);
Math.floor(1.6);
向上取整 Math.ceil(num);
Math.ceil(1.2);
Math.ceil(1.7);

3.保留整數部分parseInt()函數

parseInt(1.256);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章