Rounding decimals


#1

I have been following the treads about rounding to integers. And I get how to code for that. So I can use a loop to round decimal numbers to their nearest whole number (20.345 > 20, 18.75>19) ... Is their a way however of rounding to the nearest tenth or hundredth? or other decimal place or place value locations for that matter?


#2

This post was flagged by the community and is temporarily hidden.


#3

If I undertand this question, you can do that. You have to use the divided by block. Ex, if you wanted it to increase value by 0.5 you can do increase value by 1 divided by 2.


#4

So use a similar code block set up with the divide addition. I'll try that. Thx.


#5

Tried this and I couldn't get it to world as I had hoped. Used the code blocks suggested for rounded to whole numbers on the forum but changed where it said to add one to add .5 or rather add 1 divided by 2. Got strange results.


#6

Scrapped idea

I'm also thinking that you could do something with the + block, like

Set value (TestValue) to ((0.76) + (0.002)

But then again...

Never mind! That won't work!


Real idea

Like @SmileyAlyssa said, I think the division block would work! :wink:


#7

Hmmm... Let me go do some testing.


#8

You don't need a loop to round values.

(1) If you just want to throw-away the decimal part of a number, then assign that number to some property of an object that only preserves integer values, and then read it back. An example of such a property would be "x_position". Other value-types can work, too. Try "invisibility", if you want. Try "speed". I use that one a lot.

(2) I don't remember whether the above is more like an INT() function, or whether it's more like a ROUND() function. It might already work as a ROUND(). If not, and if you want to "round" then add 0.5 to the number first, then do what I said in #1.

EDIT: I forgot to say something about the last part of the question. If you want to round to the nearest tenth or hundredth, etc., then, first multiply the number by 10 or 100 or whatever. then do the above, then divide by 10 or 100, etc.


#9

Okay I have something that sort of working. I take my fractional number... In my case I have a value called UserInput that holds a decimal value calculated from a numerator and denominator value. Then this is passed to a rounding routine that doest rounds to the nearest 10th:

  • multiple input by 10
  • add ½ to this value
  • multiply this value by 1/10

This seems to work. But now I need a way to drop the trailing numbers.

My example is 7/8 which as a decimals is 0.875 When I run it through the above the display shows 0.925. The 9 in the 10th position is correct. But I need to limit the display to just the 10ths. Any ideas here?


#10

By assigning my decimal to the property of another object am I using the set value command? I can't seem to place the speed on the left side of the assignment... I think I am missing something.


#11

Ok. Let's say we're using "speed" to do this. Understand, by the way, this is all a hack - a work-around because the creators didn't bother including some basic functions. So, of course, it's going to be a little clunky.

So, it's not going to be one of those yellow blocks. Use the RED block to "Set Speed" to the value, with all its ugly decimals, etc. Now, the value is in. Then follow that operation with a yellow block that says "Set value" (of whatever) to the dedicated variable "speed". What you should get back is an integer value. And, if not, then do the same thing with "invisibility" or position or whatever.

It's there. Go look for it.