【发布时间】:2015-09-09 06:25:07
【问题描述】:
代码运行良好,直到我在函数内添加该行
parseLocalFloatCnt: num = Math.round(num*1.2);
有谁知道如何解决这个问题?谢谢
<!DOCTYPE html>
<html>
<body>
<p>Write something in the text field to trigger a function.</p>
<input type="text" id="myInput" oninput="myFunction()">
<p id="demo"></p>
<script>
function myFunction() {
var x = parseLocalFloatCnt(document.getElementById("myInput").value);
document.getElementById("demo").innerHTML = "You wrote: " + x;
}
function parseLocalFloatCnt(num) {
num = Math.round(num*1.2);
return +(num.replace(getLocalDecimalSeparator(), '.'));
}
function getLocalDecimalSeparator() {
var n = 1.1;
return n.toLocaleString().substring(1,2);
}
</script>
</body>
</html>
【问题讨论】:
-
你能解释一下你想做什么吗?
-
在你的函数中:
Math.roundreturn Number,你尝试做toString替换分隔符,然后再次尝试获取相同的 Number,但是为什么???如果你这样做return Math.round(num*1.2)结果将是相同的 -
另见post
标签: javascript jquery html