how to get exact two digits after decimal point in jquery or javascript

438 Views Asked by At

I want the exact 2 digits after the decimal point. I tried the toFixed(2) function but it returns rounded off 2 digits. Here is my Code it returns 18.70 but I want 18.69extract number

rim_weight = (23* 36 *70);
rim_weight = rim_weight/3100; // retuns 18.69677419354839
rim_weight = rim_weight.toFixed(2); // returns 18.70
2

There are 2 best solutions below

1
Nick On

If you just want to truncate the number to two decimal places, first divide by 31, convert to an integral value using Math.floor and then divide by 100:

rim_weight = (23 * 36 * 70);
rim_weight = Math.floor(rim_weight / 31);
rim_weight = rim_weight / 100;
console.log(rim_weight);

0
Luan CT On

just convert it to string then match the number up to the second place