当前位置: 首页 > news >正文

小程序保留2位小数据,不四舍五入

 方法1: parseInt + toFixed

/*
* 保留2位小数,不四舍五入
* 5.992550 =>5.99 , 2 => 2.00
* */
const toFixed2Decimal = (value) => {return (parseInt(value*100)/100).toFixed(2)
}
console.log(587.67*100)
console.log(toFixed2Decimal(587.67))
console.log(toFixed2Decimal(-0.1123456))
console.log(toFixed2Decimal(-1))
console.log(toFixed2Decimal('-12.999'))
console.log(toFixed2Decimal('-12.99999'))
module.exports = {toFixed2Decimal: toFixed2Decimal
}

输出 :

58766.99999999999
587.66

-0.11
-1.00
-12.99
-12.99

 有问题: 587.67 转换之后 587.66 

方法2:Math.floor + tofixed

const toFixed2Decimal = (value) => {return (Math.floor(value*100)/100).toFixed(2)
}console.log(toFixed2Decimal(-0.1123456))
console.log(toFixed2Decimal(-1))
console.log(toFixed2Decimal('-12.999'))
console.log(toFixed2Decimal('-12.99999'))

输出:

-0.12
-1.00
-13.00
-13.00  

有问题-12.99 得出-13.00 

想哭啊。2种方法出来的钱的值都不对。

http://www.lryc.cn/news/124361.html

相关文章:

  • 【linux-nginx】nginx限流以及控制访问方法
  • 菜单和内容滚动的联动原理及代码
  • Python爬虫:单线程、多线程、多进程
  • 超强的Everything,吊打系统自带文件搜索功能!
  • flink配置参数
  • 学习Vue:安装Vue.js和设置开发环境
  • 代理技术在网络安全、爬虫和数据隐私中的多重应用
  • Python 3 使用Hadoop 3之MapReduce总结
  • KU Leuven TU Berlin 推出“RobBERT”,一款荷兰索塔 BERT
  • Postern中配置和使用Socks5代理指南
  • android 窗口级模糊实现方式
  • 面试热题(数组中的第K个最大元素)
  • HTTP2协议介绍
  • 矩阵的转置
  • web集群学习:nginx+keepalived实现负载均衡高可用性
  • MFC第二十九天 CView类的分支(以及其派生类的功能)、MFC六大关键技术
  • SpringBoot复习:(37)自定义ErrorController
  • Linux学习之防火墙概述
  • JS_围绕圆形滑动
  • Ubuntu上安装RabbitMQ
  • 统计学和机器学习之间的联系和区别
  • linux中profile.d和profile的区别
  • MobaXterm sftp 不能拖拽文件夹了?
  • 【ArcGIS Pro二次开发】(59):Editing(编辑)模块
  • WebSocket与消息推送
  • 5.1 web浏览安全
  • (六)Unity开发Vision Pro——词汇表
  • 算法随笔:图论问题之割点割边
  • 【虚幻引擎】UE5数字人的创建
  • 算法:深度优先遍历