javascript - 如何使用 Jquery 对货币进行四舍五入

标签 javascript jquery math currency

如何使用 jquery 汇总不同类型的货币?我想做的是;

10.30 → 10 //Below .5 will round down
10.60 → 11 //After .5 will round up
849.95 → 850
1,022.20 → 1022

最佳答案

您可以使用Math.round()

The Math.round() function returns the value of a number rounded to the nearest integer.

请注意:带逗号的数字是字符串,您必须删除逗号。然后你可以使用 Number() 将其转换回数字,最后实现 Math.round()

console.log(Math.round(10.30));// → 10 //Below .5 will round down
console.log(Math.round(10.60))// → 11 //After .5 will round up
console.log(Math.round(849.95));// → 850
console.log(Math.round(Number('1,022.20'.replace(/,/g, ''))));// → 1022

关于javascript - 如何使用 Jquery 对货币进行四舍五入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59344580/

相关文章:

java - 指数到十进制java android

mysql - 计算特定月份的平均支出(支出金额/人)mysql

javascript - 为什么保存的数组的值会改变?

javascript - jQuery Load 不是加载 jQuery 函数

javascript - 如何从 SAPUI5 Controller 向 Java Servlet 发出 POST 请求?

jquery - 从定位标签左侧开始定位 div

javascript - 为什么 jquery addClass 和 removeClass 只能与警报功能一起正常工作?

java - 用模数求解Java中的方程

javascript - 嵌套导航点击逻辑问题

javascript - 正则表达式单词边界空间