How do you round up?

If i have a number and divide it by 10, it may create a number with a decimal. What if I cannot use decimal numbers? Is there a way to round up to ones or tens?

example:

set x to 55
set y to x/10
repeat y times
…do something
end repeat

cannot repeat 5.5 times

Hi bonedoc,

Try these examples:


set x to 55
set y to round x / 10 rounding up
set z to round x / 10 rounding down
set q to x / 10 as integer

CarbonQuark

Example for rounding to 10s 100s 1000s


set x to 5523
set y to (round (x / 10) / 10 rounding up) * 10 -- nearest 10
set z to (round (x / 10) / 100 rounding up) * 100 -- nearest 100
set q to (round (x / 10) / 1000 rounding up) * 1000 -- nearest 1000
return y & z & q

AppleScript dictionaries are your friend, bonedoc.

Check out the ‘round’ function in Standard Additions:

set x to 55
set y to round x / 10 rounding up
--> 6

There are various forms of rounding available from the same function:

Then there’s:

set x to 55
set y to (x + 5) / 10 as integer
--> 6

You could also use ‘div’:

set x to 55
set y to (x + 9) div 10
--> 6

… which is even faster and simpler when rounding down:

set x to 55
set y to x div 10
--> 5

For decimals you could try:


RoundDecimal(8.378, 1, down)

on RoundDecimal(NumberToRound, DecimalPlace, UpDown)
	set RoundFactor to 10 ^ DecimalPlace
	NumberToRound * RoundFactor
	round result rounding UpDown
	result / RoundFactor
end RoundDecimal