JavaScript小数保留两位
在JavaScript中,处理小数时经常需要保留特定位数的小数。本文将介绍如何使用JavaScript来保留小数的两位。
方法一:toFixed()
toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。
let num = 10.235;
let result = num.toFixed(2);
console.log(result); // 输出 10.24
Output:

方法二:Math.round()
Math.round() 方法可把一个数字舍入为最接近的整数。
let num = 10.235;
let result = Math.round(num * 100) / 100;
console.log(result); // 输出 10.24
Output:

方法三:parseFloat() 和 toFixed()
结合使用 parseFloat() 和 toFixed() 方法来保留小数的两位。
let num = 10.235;
let result = parseFloat(num.toFixed(2));
console.log(result); // 输出 10.24
Output:

方法四:正则表达式
使用正则表达式来保留小数的两位。
let num = 10.235;
let result = num.toString().match(/^\d+(?:\.\d{0,2})?/)[0];
console.log(result); // 输出 10.23
Output:

方法五:自定义函数
编写一个自定义函数来保留小数的两位。
function formatDecimal(num) {
  return Math.round(num * 100) / 100;
}
let num = 10.235;
let result = formatDecimal(num);
console.log(result); // 输出 10.24
Output:

方法六:Intl.NumberFormat
使用 Intl.NumberFormat 对象来格式化数字。
let num = 10.235;
let result = new Intl.NumberFormat('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 }).format(num);
console.log(result); // 输出 10.24
Output:

方法七:toFixed() 和 parseFloat()
结合使用 toFixed() 和 parseFloat() 方法来保留小数的两位。
let num = 10.235;
let result = parseFloat(num.toFixed(2));
console.log(result); // 输出 10.24
Output:

方法八:Math.floor()
使用 Math.floor() 方法向下取整来保留小数的两位。
let num = 10.235;
let result = Math.floor(num * 100) / 100;
console.log(result); // 输出 10.23
Output:

方法九:Math.ceil()
使用 Math.ceil() 方法向上取整来保留小数的两位。
let num = 10.235;
let result = Math.ceil(num * 100) / 100;
console.log(result); // 输出 10.24
Output:

方法十:自定义函数
编写一个自定义函数来保留小数的两位。
function formatDecimal(num) {
  return Math.ceil(num * 100) / 100;
}
let num = 10.235;
let result = formatDecimal(num);
console.log(result); // 输出 10.24
Output:

方法十一:toFixed() 和 parseFloat()
结合使用 toFixed() 和 parseFloat() 方法来保留小数的两位。
let num = 10.235;
let result = parseFloat(num.toFixed(2));
console.log(result); // 输出 10.24
Output:

方法十二:Math.floor()
使用 Math.floor() 方法向下取整来保留小数的两位。
let num = 10.235;
let result = Math.floor(num * 100) / 100;
console.log(result); // 输出 10.23
Output:

方法十三:Math.ceil()
使用 Math.ceil() 方法向上取整来保留小数的两位。
let num = 10.235;
let result = Math.ceil(num * 100) / 100;
console.log(result); // 输出 10.24
Output:

方法十四:自定义函数
编写一个自定义函数来保留小数的两位。
function formatDecimal(num) {
  return Math.ceil(num * 100) / 100;
}
let num = 10.235;
let result = formatDecimal(num);
console.log(result); // 输出 10.24
Output:

方法十五:toFixed() 和 parseFloat()
结合使用 toFixed() 和 parseFloat() 方法来保留小数的两位。
let num = 10.235;
let result = parseFloat(num.toFixed(2));
console.log(result); // 输出 10.24
Output:

方法十六:Math.floor()
使用 Math.floor() 方法向下取整来保留小数的两位。
let num = 10.235;
let result = Math.floor(num * 100) / 100;
console.log(result); // 输出 10.23
Output:

方法十七:Math.ceil()
使用 Math.ceil() 方法向上取整来保留小数的两位。
let num = 10.235;
let result = Math.ceil(num * 100) / 100;
console.log(result); // 输出 10.24
Output:

方法十八:自定义函数
编写一个自定义函数来保留小数的两位。
function formatDecimal(num) {
  return Math.ceil(num * 100) / 100;
}
let num = 10.235;
let result = formatDecimal(num);
console.log(result); // 输出 10.24
Output:

方法十九:toFixed() 和 parseFloat()
结合使用 toFixed() 和 parseFloat() 方法来保留小数的两位。
let num = 10.235;
let result = parseFloat(num.toFixed(2));
console.log(result); // 输出 10.24
Output:

方法二十:Math.floor()
使用 Math.floor() 方法向下取整来保留小数的两位。
let num = 10.235;
let result = Math.floor(num * 100) / 100;
console.log(result); // 输出 10.23
Output:

极客笔记