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?
1 | while true do |
2 | wait( 0.5 ) |
3 | local Br = script.Parent.Parent.Parent.Parent.neon.SpotLight.Brightness |
4 | script.Parent.Text = ( "B:" ..Br) |
5 | end |
Hello! Hope your having a good day! You can do this by using string.format like so:
1 | local spotlight = script.Parent.Parent.Parent.Parent.neon.SpotLight |
2 | local decimalCount = 2 |
3 |
4 | while true do |
5 | local brightness = spotlight.Brightness |
6 | script.Parent.Text = string.format( "%." .. (decimalCount) .. "f" , brightness) |
7 | wait(. 5 ) |
8 | 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:
1 | function truncate(num, digits) |
2 | local mult = 10 ^(digits) |
3 | return math.modf(num*mult)/mult |
4 | 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
1 | function round(num, numDecimalPlaces) |
2 | local mult = 10 ^(numDecimalPlaces or 0 ) |
3 | return math.floor(num * mult + 0.5 ) / mult |
4 | end |
5 |
6 | round(brightness) |