四舍五入数值 () `四捨五入數值 ()` 类别 运算类积木 形狀 椭圆形积木

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 rounded —this block will do the job.

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

• Calculators that will let users round numbers
```定義 calculate

變數 [answer v] 設為 ((value1) + (value2))
end

變數 [answer v] 設為 (四捨五入數值 (value))
end
. . .

calculate :: custom
```
• Checking if numbers are approximately equal to each other
```變數 [close? v] 設為 <([abs v] 數值 ((四捨五入數值 (value1)) - (四捨五入數值 (value2)))) < (2)> // one of many methods
```
• Rounding a score to the nearest whole number in games
```變數 [score v] 設為 (四捨五入數值 (score))
```
• Removing the decimal from a score, without rounding up
```變數 [score v] 設為 (四捨五入數值 ((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
```重複 (四捨五入數值 (2.3840462816)) 次
. . .
end
```
```當 @greenflag 被點擊

定位到 x: ((四捨五入數值 ((滑鼠游標的 x) / (25))) * (25)) y: ((四捨五入數值 ((滑鼠游標的 y) / (25))) * (25))
end
```

应用

Main article: List of Block Workarounds

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] 設為 [ ]

變數 [report v] 設為 (字串組合 (report) 和 (字串中第 (count) 字\( (num) \)))
變數 [count v] 改變 (1)
end

變數 [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:

`<round (number) to the nearest (base) // category=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)))) / ([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.[1] It would look like this:

`([round v] of ())`

参考

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