1

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>
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
user75521
  • 59
  • 2
  • 7
  • 1
    can you explain what you try to do? – Grundy Sep 09 '15 at 06:52
  • in your function: `Math.round` return _Number_, you try do `toString` replace separator, and again try get same _Number_, but why??? if you do `return Math.round(num*1.2)` result would be same – Grundy Sep 09 '15 at 06:56
  • also see this [post](http://stackoverflow.com/questions/2085275/what-is-the-decimal-separator-symbol-in-javascript) – Grundy Sep 09 '15 at 06:58

2 Answers2

3
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(), '.'));
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
0

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>
guvenckardas
  • 738
  • 4
  • 8