Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to round the brightness value?

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

✓ Accepted answer

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

5 comments

0

if the brightness changes using another script, it will just freeze on the last number.

0

No? Why's that?

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

0

Where is the script located?

0

I updated my post of how to set it up, serverscripts don't work in StarterGui as it's local.

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

2 comments

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 :)

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

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

function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

round(brightness)

This discussion is preserved as it was. New answers, comments, and votes are closed.