Discuss Scratch

BPert
Teacher
6 posts

How do I round numbers?

I have a question that I could not solve. I have a program to count things (https://scratch.mit.edu/projects/314458593/)
My problem is that I get too many numbers after the point (10.345678). I found the round function, but that gives me only the whole numbers. I would like to have to digits afters the point.
Could someone help me please?
Thank you very much!
E_Equals_EmCeCube3
Scratcher
1000+ posts

How do I round numbers?

For rounding to two decimal places, you can use this:
((0.01) * (round ((number) / (0.01))))

More generally, if you'd like to round numbers to a nearest value:
((to the nearest) * (round ((number) / (to the nearest))))

Is this what you're looking for?
CatsUnited
Scratcher
1000+ posts

How do I round numbers?

This is more something that belongs in the Help with Scripts subforum, but anyway
The simplest way to round something while still having digits after the point is by doing
((round ((value) * (10))) / (10))
And you can change how many more digits it rounds to by adding or removing zeroes from the multiply and divide signs.
For example, if the value is equal is 104.238, then that script above will abbreviate it to 104.2. If the two numbers that say 10 get replaced with a 100, the result would be 104.24
BPert
Teacher
6 posts

How do I round numbers?

Thank you very much! And sorry for posting in the wrong place!
rajeevg
Scratcher
11 posts

How do I round numbers?

Thank you so much, I was making a calculator and I wanted the round function.
SmellyMonkey43
Scratcher
1000+ posts

How do I round numbers?

rajeevg wrote:

Thank you so much, I was making a calculator and I wanted the round function.
I'm glad you found this helpful but please don't post on old topics.
thecrazyspinners
Scratcher
3 posts

How do I round numbers?

how do you make it round down, even if the number is higher than 0.5?
mrcreatorluigi
Scratcher
1000+ posts

How do I round numbers?

thecrazyspinners wrote:

how do you make it round down, even if the number is higher than 0.5?
Please create a topic instead of posting on an old topic, which is considered necroposting!

Last edited by mrcreatorluigi (Aug. 10, 2021 19:25:43)

-Accio-
Scratcher
1000+ posts

How do I round numbers?

thecrazyspinners wrote:

how do you make it round down, even if the number is higher than 0.5?
There is a block in the operators section that looks like this:
([abs v] of ())
If you select the arrow on that block, you will get a dropdown. In that dropdown, select the “floor” option, so that the block looks like this:
([floor v] of ())
The floor function removes any decimal places, making it so that your number always rounds down. (eg. 1.1, 1.5, and 1.9 would all floor to 1)

In the future, please create a new topic instead of posting on an old, resolved one. This ensures that your topic gets the attention that it deserves!

Powered by DjangoBB