vue關於數字的處理(四捨五入,向上取整,向下取整。。)
(38.89768).toFixed(2)
Number(parseFloat(34.3232))
截取小數點後面的兩位,四捨五入。
關於數字的處理(四捨五入,向上取整,向下取整。。)
在vue項目中可以新建一個filter.js的文件 在文件中將這些過濾全部寫進去,再在頁面進行引用
1、時分秒的顯示
格式 00:00:00
export const timeFilter = val => {
function p(t) {
return t < 10 ? ‘0’ + t: t;
}
var h = Math.floor(val/1000/60/60);
var m = Math.floor(val/1000/60%60);
var s = Math.floor(val/1000%60);
var str = p(h) + ‘:’ + p(m) + ‘:’ + p(s);
return str
}
使用:
{{timeFilter(fileInfo.needTime) || ‘’}}
2、向下取整的函數
Math.floor();
例如:Math.floor( 23.2222222); // 23
3、向上取整
Math.ceil();
例如: Math.ceil(23.333333); // 24
4、四捨五入
Math.round();
例如:Math.round(23.33333); // 23
5、四捨五入取n位小數,運算後得到的是字符串
().toFixed(n); // 取小數點後n位
例如:(36.36498524).toFixed(3); // 36.365
ps:雖然寫的是vue,但是不僅僅vue中可以使用哦