5.100000 to 5.1

Hello, I’m making a math game, in / (divide) questions answers can be 5.1000000, its hard to write so how can I make it 5 or 5,1 from a script?

You can use math.floor to get numbers like 5.1234 to 5, or math.ceil to get the number to the next whole number, like 5.1234 to 6.

math.floor(5.1) --5

OR

If you want to like reduce the decimals to only hundredths, then you can have a function like this:

function truncate(x)
    return x-x%0.01
end
1 Like

Use formatting:

--if you have float types
local formatStr = "%.1f" 
print(formatStr:format(5.1000)) --5.1 [number]

--if you have string types
local formatStr = "%.1s"
print(formatStr:format("5.1000")) --5.1 [string]

f specifies float values while s is for string. For the desired precision enter, %.x where x is the precision number (here it’s 1).

More about formatting can be found here

1 Like

you can use math.round(5.1000000) math | Documentation - Roblox Creator Hub

math.floor(5.100000000 * 100) / 100

local number = 5.100000

function RoundTenths(int)
    return math.floor(10 * int) / 10
end

print(RoundTenths(number)) -- prints 5.1