skip to Main Content

I can’t have both of them at the same time, please help.

First Attempt only Shows Decimal Places
num.toFixed(2).toLocaleString();

Second Attempt doesn’t show anything

var total = sum.toLocalString();
document.getElementById("bro123").value = total.toFixed(2);
  

2

Answers


  1. You need to change the code like this

    var total = sum.toFixed(2);
    document.getElementById("bro123").value = total.toLocaleString();
    

    toFixed doesn’t work for string.

    Login or Signup to reply.
  2. Here is a an abstract function that contains the logic you described above, if i understood correctly.
    This functions accepts both number & string as parameter, and then always convert it to float with 2 decimal points and in the end to string.

    function convertToStringWithTwoDecimals(input) {
      return Number.parseFloat(input).toFixed(2).toString();
    }
    
    convertToStringWithTwoDecimals('1234.45988'); // input as string
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search