jQuery Replace dot to comma and round it
Try this:
var price1 = 1.99234;
// Format number to 2 decimal places
var num1 = price1.toFixed(2);
// Replace dot with a comma
var num2 = num1.toString().replace(/\./g, ',');
A solution to round and replace with a class selector. This code formats 10.0 to 10,00
$('.formatInteger').each(function(){
let int = parseFloat($(this).text()).toFixed(2)
$(this).html(int.toString().replace(".", ","))
})
You can use ".toFixed(x)" function to round your prices:
price1 = price1.toFixed(2)
And then you can use method ".toString()" to convert your value to string:
price1 = price1.toString()
Also, you can use method ".replace("..","..")" to replace "." for ",":
price1 = price1.replace(".", ",")
Result:
price1 = price1.toFixed(2).toString().replace(".", ",")
Updated answer
.toFixed already returns a string, so doing .toString() is not needed. This is more than enough:
price1 = price1.toFixed(2).replace(".", ",");