Percentage Subtractor always returns numbers slightly over the correct value

  1. What do you want to achieve?
    For the percentage calculator to correctly function and return the correct values

  2. What is the issue?
    It always returns numbers very slightly over the correct value, im talking like

0.00000000003

Area
3. What solutions have you tried so far?
I got this formula off a formula website, and it seems to be correct, is this just that “Lua can’t really count good” issue or am I doing this wrong?

-- initial function
function MinusPercentage(number, percentage)
	local firstnum = percentage / 100
	local secondnum = 1 - firstnum
	local thirdnum = secondnum * number
	return thirdnum
end

--requesting line
print(MinusPercentage(500, 70)) --Should return 150, returns 150.00000000000003

I suggest the only solution to this problem is to just round it up or down using math.round(n*DecimalPlace) / DecimalPlace

What is “n” and “DecimalPlace”? Are they values I assign?

N is the number you are wanting to round such as the 150.00000000000003, also make sure to make the decimal place a value such as the tenth place - 10 , hundredth place - 100, thousandth place - 1000, and so on.

So do I just do math.round(thirdnumber)?

If you don’t want any decimal places you can use this:



-- initial function
function MinusPercentage(number, percentage)
	local firstnum = percentage / 100
	local secondnum = 1 - firstnum
	local thirdnum = secondnum * number
	return string.match(thirdnum, "%d+") -- Makes sure it's only a integer
end

--requesting line
print(MinusPercentage(500, 70)) --Should return 150, returns 150.00000000000003




If you want decimal places. You can round like he says.
Find help on math.round here: math

Yes but you also need to multiply in the parenthesis and then divide it by the same number that you multiplied with. I suggest to use a number such as 100000000 because its higher than the ten-thousandth place

So this?

	return math.round(thirdnum * 100000000 / 100000000 )

No. return math.round(thirdnum * 1e5) / 1e5 is what you’re looking for.

1 Like