This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-04-02 05:47:47

Ibzz
New Scratcher
Registered: 2012-04-02
Posts: 2

Help with rounding down to whole numbers.

I'm scripiting a dice game which involves dividing the number on the 12 sided dice by the number on the 4 sided dice for example 2 / 4 = 0.5. I'm trying to make it round down to the nearest whole number to the results that involve decimal places. If you can please help it will be much appreciated.

Offline

 

#2 2012-04-02 06:01:48

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Help with rounding down to whole numbers.

Rounding down to the nearest whole number is called the Floor Function, and is abbreviated as [x]. (Coincidentally, rounding up to the nearest whole number is called the Ceiling function). Here are the stacks you need:

when gf clicked 
set [x v] to [3.14]
say (round ((x)-(0.5)))//Floor function = 3
say (round ((x)+(0.5)))//Ceiling function = 4
say (round (x))//Normal round function = 3

Last edited by Hardmath123 (2012-04-02 06:02:00)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#3 2012-04-02 06:12:15

Ibzz
New Scratcher
Registered: 2012-04-02
Posts: 2

Re: Help with rounding down to whole numbers.

Sorry, I'm not that great with scratch.. Why is it 3.14? All i need to use is the floor function. Also the -0.5 etc wouldnt work because if i rolled a 7 on the 12 sided dice and a 3 on the 4 sided dice it would get 2.333??... I want it so that it rounds to 2 (an interger).

Sorry you probably explained it above, I just can't understand it ;ss.

Offline

 

#4 2012-04-02 06:56:15

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: Help with rounding down to whole numbers.

Ok, simply put, rather than say (round (2.33333)) you would use (round ((2.33333)-(0.5))). In math-y terms, just subtract 0.5 from the number, then ound it to get the floor of that number. Get it? I admit it, I could have been clearer in the first post.  wink

Last edited by Hardmath123 (2012-04-02 06:58:02)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#5 2012-04-03 12:30:00

rdococ
Scratcher
Registered: 2009-10-11
Posts: 1000+

Re: Help with rounding down to whole numbers.

Be careful, through. Althrough 3 rounded up equals 3, the +0.5 method will return 4.

This method works best:

when I receive [round up v]
if <(round (number)) = (number)>
set [rounded number v] to (number)
else
set [rounded number v] to (round ((number) + (0.5)))
end
Beware: This error also occurs if you round down a negative number. To fix this, use this code:

when I receive [round down v]
if <(round (number)) = (number)>
set [rounded number v] to (number)
else
set [rounded number v] to (round ((number) - (0.5)))
end
Also, this simpler equation will have no errors too:

((number) - ((number) mod (1)))

Last edited by rdococ (2012-04-03 12:32:28)

Offline

 

Board footer