Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What causes numbers to appear like this and how can I fix it?

Asked by 8 years ago
Edited 8 years ago

This is pretty much how the script goes:

1brightness = blahblahblah.Part.SpotLight
2 
3while wait(0.1) do
4    script.Parent.Text = (brightness.Brightness)
5end

The brightness appears as something like 4.5899906158447 when displayed on the TextLabel but in the SpotLight properties it doesn't, I want it to appear like say 4.5 instead.

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago
Edited 8 years ago

To fix this, simply round your number.

1script.Parent.Text = math.floor(brightness.Brightness + .5)
  • math.floor function rounds a decimal down.

  • +.5 because a decimal should only be rounded down if it's below .5 --> adding .5 makes it so if the number was per say 4.55, then it would change to 5.05 and be rounded down to 5

If you don't want it to round full numbers, you can multiply the Brightness by 10^x, where x is your decimal place, then divide it by the same factor on the outside of the floor function.

1--round to 10ths place
2script.Parent.Text = math.floor((brightness.Brightness*10)+.5)/10

Here's a neat function for rounding to specified decimal places :)

1function round(num,place)
2    local divisor_and_factor = 10^place;
3    return math.floor((num*divisor_and_factor)+.5)/divisor_and_factor;
4end
5 
6--rounds 2 decimal places in
7script.Parent.Text = round(brightness.Brightness,2);
0
Ugh, and I was just looking through the mathematical functions page on the wiki too. Don't know how I missed it, thanks! crabbyninja 58 — 8y
Ad

Answer this question