I have already tried the following:
discval = 2.833423
discval = discval.toFixed(2).toString().replace("." , ",");
discval = parseFloat(discval);
The output is 2 and not 2,83
Any idea?
parseFloat("2,83")
will return 2
because ,
is not recognized as decimal separator, while .
is.
If you want to round the number to 2 decimal places just use parseFloat(discval.toFixed(2))
or Math.round(discval * 100) / 100;
If you need this jut for display purposes, then leave it as a string with a comma. You can also use Number.toLocaleString()
to format numbers for display purposes. But you won't be able to use it in further calculations.
BTW .toFixed()
returns a string, so no need to use .toString()
after that.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments