[Solved] Rounding up by Multiple of 100

HI,
I’d like to round my calculation to the nearest 100 (upward) , so if my calculation is 167 it should be 200 , if it’s 540 it’s 600.

Is this possible?

I don’t know if there’s a better way, but what about this (with $MyNumber as the original number, $MyAnswer as, well, the answer… :grinning:)

$MyAnswer = ceil($MyNumber/100) * 100

Ceil rounds up to the next integer. I’ve not tested extensively, but it works for tens, hundreds and thousands etc. It still round up for negative numbers (-333 becomes -300), but you can test for that if it’s important (if the number is negative, use floor() instead of ceil()).

There may be other edge cases and I’m not good at maths, so treat this with caution and test!

  • Thank you so much ! I will indeed check out this through some test .

  • again, Thanks for quick revert

If you are trying to round up whole numbers, you can convert them to decimals and then use the round() operation.

There are also other number formation operations like .percision and .format that you can take a look at.

http://www.acrobatfaq.com/atbref8/index/ActionsRules/Operators/FullOperatorList/NumberprecisionN.html

My preference here is a simple idiom:

$MyNumber=100*round($MyNumber/100)

I noticed that you wanted to “round up”, then you’d prefer

$MyNumber=100*ceil($MyNumber/100)

To round down:

$MyNumber=100*floor($MyNumber/100)

2 Likes

Dear @Brookter, thanks for the explanation , you math is good indeed !!
@satikusala, thanks for the stamp idea , I did look t precision and .format , thank you
@eastgate, thank you for your idioms !!

A breakout of maths-related functions can be seen here.

Drawing on this thread I’ve added some more examples to the note on round().

2 Likes

Thank you for adding the examples . I did visit round and couldn’t relate to simple calculations above . It will help many people !

2 Likes