简体   繁体   English

javascript - 一美元金额的上限

[英]javascript - ceiling of a dollar amount

So I am adding and subtracting floats in javascript, and I need to know how to always take the ceiling of any number that has more than 3 decimal places. 所以我在javascript中添加和减去浮点数,我需要知道如何始终取任何小数超过3位数的数字的上限。 For example: 例如:

3.19 = 3.19 3.19 = 3.19

3.191 = 3.20 3.191 = 3.20

3.00000001 = 3.01 3.00000001 = 3.01

num = Math.ceil(num * 100) / 100;

Though, due to the way floats are represented , you may not get a clean number that's to two decimal places. 虽然, 由于浮点数的表示方式 ,你可能得不到一个小数点后两位的干净数字。 For display purposes, always do num.toFixed(2) . 出于显示目的,请始终执行num.toFixed(2)

Actually I don't think you want to represent dollar amounts as float, due to the same reason cited by Box9. 实际上,由于Box9所引用的相同原因,我认为您不想将美元金额表示为浮动金额。 For example, 0.1*3 != 0.3 in my browser. 例如,在我的浏览器中0.1 * 3!= 0.3。 It's better to represent them as integers (eg cents). 最好将它们表示为整数(例如美分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM