Var*_*ada 110 javascript string-conversion
我们如何将JavaScript字符串变量转换为十进制?
有没有这样的功能
parseInt(document.getElementById(amtid4).innerHTML)
Run Code Online (Sandbox Code Playgroud)
lon*_*day 231
是的 - parseFloat.
parseFloat(document.getElementById(amtid4).innerHTML);
Run Code Online (Sandbox Code Playgroud)
要格式化数字,请使用toFixed:
var num = parseFloat(document.getElementById(amtid4).innerHTML).toFixed(2);
Run Code Online (Sandbox Code Playgroud)
num 现在是一个字符串,其数字格式为两位小数.
Koo*_*Inc 63
您还可以使用Number构造函数/函数(不需要基数,可用于整数和浮点数):
Number('09'); /=> 9
Number('09.0987'); /=> 9.0987
Run Code Online (Sandbox Code Playgroud)
小智 10
这有效:
var num = parseFloat(document.getElementById(amtid4).innerHTML, 10).toFixed(2);
Run Code Online (Sandbox Code Playgroud)
var formatter = new Intl.NumberFormat("ru", {
style: "currency",
currency: "GBP"
});
alert( formatter.format(1234.5) ); // 1 234,5 £
Run Code Online (Sandbox Code Playgroud)
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat