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
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
Math.floor(number*10)/10 any good?
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)