Round ()
round ()
Category Operator
Type Reporter

The Round () block is an Operators block and a Reporter block. The block rounds the given number to the nearest integer. It follows the standard rules of rounding; decimals that are .5 or higher are rounded up, whereas decimals less than .5 are rounded down.

Example Uses

In projects that handle decimal numbers, they may have to be rounded—this block will do the job.

Some common uses for the Round () block are below:

  • Calculators that will let users round numbers
define calculate
if <(operator) = [addition]> then
set [answer v] to ((value1) + (value2))
end
if <(operator) = [round]> then
set [answer v] to (round (value))
end
...

when this sprite clicked //calculator equivalent to the "=" button
calculate
  • Checking if numbers are approximately equal to each other
set [close? v] to <([abs v] of ((round (value1)) - (round (value2)))) < (2)> //one of many methods
  • Rounding a score to the nearest whole number in games
set [score v] to (round (score))
  • Removing the decimal from a score, without rounding up
set [score v] to (round ((score) - (0.5)))//any number minus 0.5 will round to the lower integer
  • Rounding the value to be put in the Repeat () block, since the value must be a whole number
repeat (round (2.3840462816))
...
end
when gf clicked
forever
go to x: ((round ((mouse x) / (25))) * (25)) y: ((round ((mouse y) / (25))) * (25))

Workaround

Main article: List of Block Workarounds

One can use the "Ceil" and "Floor" operations in Scratch 3.0 to accurately round a number:

([floor v] of ((number) + (0.5))) //round a number

The mathematical functions "floor" and "ceiling" can also be replicated using modular arithmetic:

((number) - ((number) mod (1))) //round down (floor)
((number) + ((1) - ((number) mod (1)))) //round up (ceiling)

or

if <(n) > (0)> then
set [rounded v] to (((n) + (0.5)) - (((n) + (0.5)) mod (1)))
else
set [rounded v] to ((((n) - (0.5)) - (((n) - (0.5)) mod (1))) + (1))
end

Another way to replicate the block is to use the letter () of () and join () () operator blocks.

set [report v] to [ ]
set [count v] to [0]
  repeat until <<(letter (count) of (num)) = [.]> or <(count) = (length of (num))>>
  set [report v] to (join (report) (letter (count) of (num)))
  change [count v] by (1)
end
if <<(letter (count) of (num)) = [.]> and <(letter ((count) + (1)) of (num)) > [4]>> then
  change [report v] by (1)
end

Suggestions

Some Scratchers want a reporter block which rounds to a certain number-place. The block would make all of the above uses more accurate. It would look like this:

(round (number) to the nearest (base)::operators)

It can be replicated by shifting the number over (multiplying or dividing by 10), rounding, and then reversing the original operation.

((round ((number) * ([10^ v] of (number of places):: operators))) / ([10^ v] of (number of places):: operators))

or even this:

((round ((round what) / (to the nearest))) * (to the nearest))

Some Scratchers have suggested to update the (round ()) block as a function.[1] It would look like this:

([round v] of ():: operators)

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.