分別從四舍五入和是否自動補0這兩個方面進行入手。
會四舍五入
1、toFixed()函數(shù)
注意:此方法將數(shù)值類型的數(shù)據(jù)改變成了字符串類型
// 四舍五入
var num = 1.6492;
num = num.toFixed(2);
console.log(num); //1.65
console.log(typeof num); //string
不會四舍五入
1.Math.floor()和Math.ceil()函數(shù)
注意:此方法不改變數(shù)據(jù)類型
var number = 1.7332;
var Test1 = Math.floor(number * 100) / 100;//保留兩位小數(shù),
var Test2 = Math.ceil(number * 100) / 100;//保留兩位小數(shù)
console.dir(Test1); //1.73
console.dir(typeof(Test1)); // number
console.dir(Test2); //1.74
console.dir(typeof(Test2)); // number
2、字符串匹配
注意:此方法先將數(shù)據(jù)轉(zhuǎn)換為字符串,最后再轉(zhuǎn)為數(shù)值類型,不會四舍五入,字符串匹配再轉(zhuǎn)換
var num = 1.6492;
// 寫法一:
num = Number(num.toString().match(/^\d+(?:\.\d{0,2})?/));
console.log(num); //1.64
console.log(typeof num); // number
// 寫法二:
let a = 12.12345678
let b = a.toString()
let c = b.substring(0, b.indexOf('.') + 3) * 1
console.log(c) //12.12
其中【字符串匹配法】好于【Math.floor()】,因為字符串能保證高精度,不受js浮點數(shù)長度的約束,同時在動態(tài)修改保留位數(shù)時更有優(yōu)勢.
3、parseInt()函數(shù)
注意:此方法不改變數(shù)據(jù)類型
var number = 0.99996;
var Test1 = parseInt(number * 100) / 100;//保留兩位小數(shù),
console.dir(Test1); //0.99
console.dir(typeof(Test1)); // number
但是,當小數(shù)位只有一位或者兩位都為0的情況下,則只會保留整數(shù)。
var number = 0.900006;
var Test1 = parseInt(number * 100) / 100;//保留兩位小數(shù),
console.dir(Test1); //0.9
console.dir(typeof(Test1)); // number
保留兩位小數(shù),若第二位小數(shù)為0,則補0(不四舍五入)
方法一:parseInt()和toFixed()
var number = 0.900006;
var Test1 = (parseInt(number * 100) / 100).toFixed(2);//保留兩位小數(shù),
console.dir(Test1); //0.90
console.dir(typeof(Test1)); // number
方法二:Math.floor()與toFixed()
var number = 0.900006;
var Test1 = (Math.floor(number * 100) / 100).toFixed(2);//保留兩位小數(shù),
console.dir(Test1); //0.90
console.dir(typeof(Test1)); // number
保留兩位小數(shù),若第二位小數(shù)為0,則補0(四舍五入)
注意:數(shù)據(jù)類型變?yōu)樽址愋?mark hidden color="red">文章來源:http://www.zghlxwxcb.cn/news/detail-559894.html
function keepTwoDecimalFull(num) {
var result = parseFloat(num);
if (isNaN(result)) {
alert('傳遞參數(shù)錯誤,請檢查!');
return false;
}
result = Math.round(num * 100) / 100;
var s_x = result.toString(); //將數(shù)字轉(zhuǎn)換為字符串
var pos_decimal = s_x.indexOf('.'); //小數(shù)點的索引值
// 當整數(shù)時,pos_decimal=-1 自動補0
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += '.';
}
// 當數(shù)字的長度< 小數(shù)點索引+2時,補0
while (s_x.length <= pos_decimal + 2) {
s_x += '0';
}
return s_x;
}
console.log(keepTwoDecimalFull(1.6042)); //1.60
console.log(typeof keepTwoDecimalFull(1.6042)); //string
保留兩位小數(shù),若第二位小數(shù)為0,則保留一位小數(shù))(不四舍五入)用Math.floor()方法
保留兩位小數(shù),若第二位小數(shù)為0,則保留一位小數(shù))(四舍五入)
注意:數(shù)據(jù)類型不變文章來源地址http://www.zghlxwxcb.cn/news/detail-559894.html
var num = 1.6042;
function keepTwoDecimal(num) {
var result = parseFloat(num);
if (isNaN(result)) {
alert('傳遞參數(shù)錯誤,請檢查!');
return false;
}
result = Math.round(num * 100) / 100;
return result;
};
keepTwoDecimal(num);
console.log(keepTwoDecimal(num)); //1.6
console.log(typeof num); //number
到了這里,關(guān)于(05)VUE/JS 保留小數(shù)方法合集 (保留兩位為例)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!