The code is working fine until I add the line inside the function
parseLocalFloatCnt: num = Math.round(num*1.2);
Does anyone know how to solve this? Thanks
<!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>
Uncaught TypeError: num.replace is not a function(…)
You can't call replace
on a number.
You could do this instead:
function parseLocalFloatCnt(num) {
num = Math.round(num*1.2) + ''; // convert `num` to string
return +(num.replace(getLocalDecimalSeparator(), '.'));
}
Don't forget num to.String();
<!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).toString();
return +(num.replace(getLocalDecimalSeparator(), '.'));
}
function getLocalDecimalSeparator() {
var n = 1.1;
return n.toLocaleString().substring(1,2);
}
</script>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.