我有一个十进制数字,显示如下:
<nested:text property="product.price" maxlength="5" onclick="javascript:validatePriceValue(this);"/>
number | displayed as
_________|_______________
44 | 44.00
我想使用javascript显示只有44而不是44.00
我该怎么办?
答案 0 :(得分:0)
也许你有一个舍入问题。没有代码和实际值,就无法分辨。
您可以尝试这种难以理解的解决方法:
// v is the value
// w is a pseudo-down-cast to an int (javascript has no ints)
w = parseInt("" + v, 10);
答案 1 :(得分:0)
试试这个:
var f = 44.00;
var i = parseInt(f);
见这个
答案 2 :(得分:0)
有多种方法可以将float转换为int:
var x = parseInt("1000.0", 10); // This ignores anything behind the decimal point. Radix 10 is technically optional, but a good habit to make sure everything gets interpreted as a decimal number.
var x = Math.floor("1000.01"); // Round down. -> 1000
var x = Math.ceil("1000.01"); // Round up. -> 1001
var x = Math.round("1000.01"); // Round to nearest int. -> 1000
答案 3 :(得分:0)
您可以使用Math.floor()
(向下舍入),Math.ceil()
(向上舍入)或Math.round()
(舍入到最接近的整数),具体取决于您希望如何删除小数。
如果您只是想截断小数部分,请使用bitwise operator(.e.g |0
)将其操作数视为带符号的32位整数。
您可能还在谈论使用浮点运算进行十进制舍入的不准确性。
谢谢,
希瓦
答案 4 :(得分:0)
如果数字存储在变量中,请说x
为字符串,如此
x = "44.00";
然后使用parseInt
函数,您将得到没有小数位的数字
y = parseInt(x);