Rounding down amounts

We are doing some calculations where the results need to be rounded down to closest integer, ten, or one-hundred (i.e. 1.99 should be rounded to 1.00, 19 should be rounded to 10 etc.)

As regards rounding to 10 or 100 i have understood that there is no built in solution so the plan is to divide the amount by 10 or 100, round the result and then multiply the amount back to achieve this. But I haven’t been able to find a good solution for rounding down so help with that part would be greatly appreciated.

Hi Bjorn,

In your workaround, try to use the floor: filter, which will round down to the nearest whole number/multiple of 10 etc. based on the floor parameter you provide. floor:0 will round down to the nearest integer and floor:-1 will round down to the nearest multiple of 10. Compare to the ceil function described in the documentation. E.g.

{% assign your_variable = 999 %}
{{ your_variable | floor:-2 }}

output: 900

In addition, we are currently discussing your case internally, and therefore we need some additional information:

  • How to deal with figures smaller than 1?
  • Do you always want to round down to the largest multiple of 10^x, e.g. 1999.999 becomes 1000 and 999 becomes 900?

Thanks in advance.

Thanks! Floor seems to work perfectly. Smaller than 1 should always be 0 and since the use in this case is to calculate taxes we will not need to consider negative amounts.

And as far as I can see we should always go down to the largest multiple so 19.99 should result in 10 etc.

1 Like