判断小数位数

2021-07-27  本文已影响0人  INGME
//小数(小于等于1)位数多余3位做处理
floatNum(num) {
   let y = String(num).indexOf('.') + 1;
   let count = String(num).length - y;

   if (y > 0 && count > 2) {
     return (num * 100).toFixed(2) + '%';
   } else {
     return num * 100 + '%';
   }
}
//保留n位小数(四舍五入)
function roundFun(value, n) {
  return Math.round(value * Math.pow(10, n)) / Math.pow(10, n);
}
//保留n位小数(不四舍五入)
function toFixed(num, n) {
   return ~~(Math.pow(10, n) * num) / Math.pow(10, n);
}
上一篇 下一篇

猜你喜欢

热点阅读