Zho:四舍五入数值 ()（积木）

The Round  积木 is an 运算类积木 and a 椭圆形积木. The block rounds the given 数字 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.

用法示例
In 专案 that handle decimal numbers, they may have to be roundedthis block will do the job.

Some common uses for the Round block are below: 定義 calculate 如果 <(operator) = [addition]> 那麼 變數 [answer v] 設為 ((value1) + (value2)) end 如果 <(operator) = [round]> 那麼 變數 [answer v] 設為 (四捨五入數值 (value)) end . ..
 * Calculators that will let users round numbers

當角色被點擊 // calculator equivalent to the "=" button calculate :: custom 變數 [close? v] 設為 <([abs v] 數值 ((四捨五入數值 (value1)) - (四捨五入數值 (value2)))) < (2)> // one of many methods 變數 [score v] 設為 (四捨五入數值 (score)) 變數 [score v] 設為 (四捨五入數值 ((score) - (0.5))) // any number minus 0.5 will round to the lower integer 重複 (四捨五入數值 (2.3840462816)) 次 . . . end 當 @greenflag 被點擊 重複無限次 定位到 x: ((四捨五入數值 ((滑鼠游標的 x) / (25))) * (25)) y: ((四捨五入數值 ((滑鼠游標的 y) / (25))) * (25)) end
 * Checking if numbers are approximately equal to each other
 * Rounding a score to the nearest whole number in games
 * Removing the decimal from a score, without rounding up
 * Rounding the value to be put in the Repeat block, since the value must be a whole number
 * Positioning the cursor in tile-based games such as Minesweeper
 * Snapping to a grid

应用
One can use the "Ceil" and "Floor" 运算 in Scratch 2.0 to accurately round a number:

([floor v] 數值 ((number) + (0.5))) // round a number The mathematical functions "floor" and "ceiling" can also be replicated using modular arithmetic:

((number) - ((number) 除 (1) 的餘數)) //round down (floor) ((number) + ((1) - ((number) 除 (1) 的餘數))) //round up (ceiling) or 如果 <(n) > (0)> 那麼 變數 [rounded v] 設為 (((n) + (0.5)) - (((n) + (0.5)) 除 (1) 的餘數))

變數 [rounded v] 設為 ((((n) - (0.5)) - (((n) - (0.5)) 除 (1) 的餘數)) + (1)) end Another way to replicate the block is to use the letter of  and join   operator blocks. 變數 [report v] 設為 [ ] 變數 [count v] 設為 [0] 重複直到 <<(字串中第 (count) 字\( (num) \)) = [.]> 或 <(count) = (字串長度\( (num) \))>> 變數 [report v] 設為 (字串組合 (report) 和 (字串中第 (count) 字\( (num) \))) 變數 [count v] 改變 (1) end 如果 <<(字串中第 (count) 字\( (num) \)) = [.]> 且 <(字串中第 ((count) + (1)) 字\( (num) \)) > [4]>> 那麼 變數 [report v] 改變 (1) end

Suggestions
Some Scratcher 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:



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)))) / ([10^ v] of (number of places))) or even this: ((round ((round what) / (to the nearest))) * (to the nearest))

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