Thread: How to round these numbers?

1. How to round these numbers?

Hello. How can I round these?

1.01 to 1.00
1.05 to 1.00
1.11 to 1.10
1.15 to 1.10
1.19 to 1.20

I basically want to get rid of the second decimal value. 12.1X and turn that to zero!

Currently, I do this but it's not exactly what I want. I lose the first decimal point as well and both first and second decimal values turns to .00
Here is my code:

Code:
```\$('#total').val(format_currency(Math.round(total)));
function format_currency(total)
{
var neg = false;
if(total < 0) {
neg = true;
total = Math.abs(total);
}
return parseFloat(total, 10).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "\$1,").toString();
}```
Thanks
Last edited by English Breakfast Tea; April 4th, 2017 at 02:02 AM.
2. Getting rid of the second decimal would turn 1.19 into 1.1...

How about just normal rounding rules: always round down, always round to zero, or always round to the nearest .1?
3. "Getting rid of the second decimal" I meant rounding it to zero. How can I do that?

"How about just normal rounding rules".

I thought that's what my code is doing 4. I said "normal rules" because of that 1.19 -> 1.2 exception. If that should be going to 1.1 instead then you do want normal rules, but of truncation (aka "rounding" to zero).

Use the standard multiply/divide trick but with Math.trunc instead of Math.round:
Code:
`Math.trunc(value * 10) / 10`
trunc and floor are the same for positive numbers but differ for negatives: trunc rounds towards zero (-1.1 -> -1) while floor rounds to -INF (-1.1 -> -2).