Roundto() seems inconsistent

HI

For the community members better educated in maths than me, should the answer in row 4 not be -200?

image

Regards
Piet

Come to think of it, shouldn’t the answer for row 1 be 136?

There’s three formulas for rounding: RoundUp(), RoundDown(), & RoundTo(). RoundTo will go to the nearest integer. So maybe try using one of the other two to get your desired result?

This works exactly as intended. It rounds to the nearest value that is a multiple of your factor.

Row 1 it round to the nearest of 134 or 135.,
Row 2 it round to the nearest of 130 or 140,
Row 3 it round to the nearest of -130 or -135,
Row 4 it round to the nearest of -100 or -200,
Row 5 it round to the nearest of -130 or -140

For sake of completeness, there are actually 8 Rounding-related formulas in Coda and they all behave in slightly different ways. The Coda School covers each of the 8 rounding formulas in length here

For the sake of simplicity, Standard rounding rules state that anything BELOW 5 will round down and anything ABOVE 5 will round up.

Heres some examples with standard rounding rules

  • 4.9 to the factor of 10 will round → 0

    • 4.9 is below the 5 threshold so it will round DOWN based on standard rounding rules. But if you were rounding 5 to the factor of 10, it will round UP to 10.
  • 4.9 to the factor of 1 will round → 5

    • In this case, we are comparing the numbers after the decimal to determine which way our number will go. So 0.9 is above the 0.5 threshold with a factor of 1, so it will round up. BUT 4.49 rounding to the factor of 1 is BELOW the 0.5 threshold so it would round DOWN to 4.
  • 1.49 to the factor of 1 will round → 1

  • 1.499 to the factor of 1 will round → 1

  • 1.5 to the factor of 1 will round → 2

  • 49 to the factor of 100 → 0

  • 50 to the factor of 100 → 100

Round and RoundTo are essentially equivalent in that they follow standard rounding rules, but RoundTo allows you to define a factor.

RoundUp and RoundDown though do NOT follow standard rounding rules. They will always round up/down so:

  • RoundUp 99 to the factor of 100 → 100
  • RoundDown 99 to the factor of 100 → 0

Thanks guys,

I had a short circuit - wanted it to calc the next largest, rather than nearest…

I’ll clean my glasses.

P

And, [could] rounded numbers be impacted by the infamous javascript floating point error as well?

Very good article. I’ve had a few problems with floating point errors in Coda.
I’m going to use this trick below from now on.
Instead of:


this:

This isn’t specific to coda, it’s a consequence of the binary floating-point system, it’s seen in every programming language. Here’s a relevant SO thread