Why is my toFixed() function not working?

前端 未结 5 1313
悲哀的现实
悲哀的现实 2020-12-29 17:49

Here\'s the relevant code. I\'ve confirmed with the alert that the correct number is saved, it\'s just not being changed to 2 decimal places.

if ($(this).a         


        
相关标签:
5条回答
  • 2020-12-29 18:11

    Your conversion data is response[25] and follow the below steps.

    var i = parseFloat(response[25]).toFixed(2)
    console.log(i)//-6527.34
    
    0 讨论(0)
  • 2020-12-29 18:15

    You're not assigning the parsed float back to your value var:

    value = parseFloat(value).toFixed(2);
    

    should fix things up.

    0 讨论(0)
  • 2020-12-29 18:17

    I tried function toFixed(2) many times. Every time console shows "toFixed() is not a function".

    but how I resolved is By using Math.round()

    eg:

    if ($(this).attr('name') == 'time') {
        var value = parseFloat($(this).val());
        value = Math.round(value*100)/100; // 10 defines 1 decimals, 100 for 2, 1000 for 3
        alert(value);
    }
    

    this thing surely works for me and it might help you guys too...

    0 讨论(0)
  • 2020-12-29 18:20

    Example simple (worked):

    var a=Number.parseFloat($("#budget_project").val()); // from input field
    var b=Number.parseFloat(html); // from ajax
    var c=a-b;
    $("#result").html(c.toFixed(2)); // put to id='result' (div or others)
    
    0 讨论(0)
  • 2020-12-29 18:28
    document.getElementById("EDTVALOR").addEventListener("change", function() {
      this.value = this.value.replace(",", ".");
      this.value = parseFloat(this.value).toFixed(2);
      if (this.value < 0) {
        this.value = 0;
      }
      this.value = this.value.replace(".", ",");
      this.value = this.value.replace("NaN", "0");
    });
    
    0 讨论(0)
提交回复
热议问题