美文网首页
判断小数位数

判断小数位数

作者: INGME | 来源:发表于2021-07-27 17:44 被阅读0次
//小数(小于等于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);
}

相关文章

网友评论

      本文标题:判断小数位数

      本文链接:https://www.haomeiwen.com/subject/zdvomltx.html