我想删除小数,只打印前两个小数值,我该怎么做。
XML
<bookstore>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>2.21595037861026842</price>
</bookstore>
如何仅打印价格标签中的前2个十进制值
x=xmlDoc.getElementsByTagName("price")[0].childNodes[0];
document.write(x.nodeValue);
结果:2.21
答案 0 :(得分:1)
x = xmlDoc.getElementsByTagName("price")[0].childNodes[0];
var z = x.nodeValue;
var y = Math.round(z * 100) / 100;
document.write(y);
答案 1 :(得分:0)
首先,将节点值字符串解析为float,然后将float舍入为2位小数。这是一步一步的例子:
x = xmlDoc.getElementsByTagName("price")[0].childNodes[0];
var string_x = x.nodeValue;
var float_x = parseFloat(string_x);
var float_x_fixed = float_x.toFixed(2);
document.write(float_x_fixed);
供参考: