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

How to round the brightness value?

Asked by 3 years ago

So i have this line of code that just sets the gui text to the brightness value of the flashlight. however it sets it to a odd 10 digit number (ex. 1.2000007312) i wish there was a way to round that number to 1.2, it works for the range value just fine but not for the brightness, how to fix?

while true do
    wait(0.5)
    local Br = script.Parent.Parent.Parent.Parent.neon.SpotLight.Brightness
    script.Parent.Text = ("B:"..Br)
end

4 answers

Log in to vote
0
Answered by
DrShockz 233 Moderation Voter
3 years ago
Edited 3 years ago

Hello! Hope your having a good day! You can do this by using string.format like so:

local spotlight = script.Parent.Parent.Parent.Parent.neon.SpotLight
local decimalCount = 2

while true do
    local brightness = spotlight.Brightness
    script.Parent.Text = string.format("%." .. (decimalCount) .. "f", brightness)
    wait(.5)
end

If the brightness was 12.1874 It would output "12.18" If you want to change the number of decimals change the decimalCount value!

EDIT: Make sure you have your script as a LocalScript in your GuiElement, otherwise it won't work.

Here are my results and setup:

Results: GIF

Setup: IMAGE

Have a great day! - DrShockz

0
if the brightness changes using another script, it will just freeze on the last number. BloxyCola_Gamer 10 — 3y
0
No? Why's that? DrShockz 233 — 3y
0
idk, it just freezes and and shows the same number forever, i checked the script incase i copypasted it wrong, if i change the brightness using another script, it will just simply freeze, also no errors in the output BloxyCola_Gamer 10 — 3y
0
Where is the script located? DrShockz 233 — 3y
0
I updated my post of how to set it up, serverscripts don't work in StarterGui as it's local. DrShockz 233 — 3y
Ad
Log in to vote
1
Answered by 3 years ago

Math.floor(number*10)/10 any good?

0
This would bring the tenths above math.floor, round the number in the example to 12 and then divide it by ten to give you 1.2 back :) AlexTheCreator 461 — 3y
0
This is exactly what I was going to answer. To apply it to the OP's code, use this after you capture the brightness: Br = math.floor(Br*10)/10 SteelMettle1 394 — 3y
Log in to vote
0
Answered by
lolzmac 207 Moderation Voter
3 years ago

You should find some success with the following code:

function truncate(num, digits) 
    local mult = 10^(digits)
    return math.modf(num*mult)/mult
end

Give it a number, in this case brightness, and the amount of decimals you want to keep, in this case 1, and it'll return the number with a truncated decimal.

eg. truncate(1.2000007312, 1) --1.2

Log in to vote
-1
Answered by 3 years ago
function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

round(brightness)

Answer this question