-1

我有一个号码

例如:

8183

我需要的是将其转换为浮点数-

例如 8183

(8183).toFixed(2);

会还给我

8183.00

但我需要进一步截断它,所以最终数字将是

8.18

所以基本上我需要让它只有 2 个小数位的浮点数。我尝试使用 Math.floor 和 ceil 但无法弄清楚!

4

2 回答 2

7

Well what you're trying to accomplish is not completely clear, but I think that if you start by dividing by 1000, then call toFixed on it, it will give you the desired result.

var before = 8183;
var after = (before / 1000).toFixed(2); //8.18
于 2012-04-30T19:39:05.743 回答
4

You could divide by 10 until you are less than 10:

var digits = 8183;
while((digits = digits/10) > 10) {}
digits = digits.toFixed(2); // 8.18

For negative numbers, you could want to store a boolean value and use Math.abs(digits).

For numbers less than 0, you would want to multiple instead of divide.


If all you really want is scientific notation use toExponential(2)

于 2012-04-30T19:40:21.007 回答