How can I keep only one decimal?

Hello. I am currently making an overhead gui that displays a player’s total RAP (recent average price of collectibles) and I made a function to round the RAP when it is over 1000. Here is the code:

function RoundRAP(Value)
	local RAPString = ""
	if Value >= 1000000 then 
		RAPString = (tostring(math.ceil(Value/1000000)) .. "M")
		return RAPString
	elseif Value >= 1000 then
		RAPString = (tostring(math.ceil(Value/1000)) .. "K")
		return RAPString
	elseif Value < 1000 then
		RAPString = (tostring(Value))
		return RAPString
	end
end

The problem I had was that my function didn’t round the decimals, so there would be too many of them. I tried to fix that with math.ceil but the problem with math.ceil is that it does not give any decimals at all, and I want one. Is there a way to only keep 1 decimal in the number or to round the number, but keep the first decimal?

1 Like

use this function

local function roundTo(a, b)
    local result = (math.floor(a / b + 0.5) * b)
    return result
end

then call the function roundTo(a, 0.1)

2 Likes

Solution can be found here, but instead of two decimals, rewrite it to one.

Thank you for your reply. I used string.format instead.
Here is the code for anyone who needs it:

function RoundRAP(Value)
	local RAPString = ""
	if Value >= 1000000 then 
		RAPString = ((string.format("%0.1f", tostring(Value/1000000))) .. "M")
		return RAPString
	elseif Value >= 1000 then
		RAPString = ((string.format("%0.1f", tostring(Value/1000))) .. "K")
		return RAPString
	elseif Value < 1000 then
		RAPString = (tostring(Value))
		return RAPString
	end
end
4 Likes

Thank you for your reply.
30chars