Taking away a decimal from a normal number results in this

I have absolutely 0 clue why simple maths like this results in this junk. This is basically an example of what is happening on my game. Literally why does it do this?? Please help me fix this
paveikslas

tostring(number):split(“.”)[1] will be what you need

paveikslas
it doesnt show the decimal now

What’re you trying to achieve? If you meant removing the decimal then that should do what you want, if you didn’t then and meant you’re just taking away 2 numbers, one with a decimal, then it’ll always print like that.

My car in my game is priced at 1500.9 and I want to make it purchasable, so my cash (2000) has 1500.9 taken away from it (2000-1500.9), and thats suppose to equal 499.1, not .09999999999… etc

you can use math.round(number)

1 Like
math.floor(print(2000-1500.9))

yours and @PeculiarMei’s solutions both still equal 499

that is how floating point numbers (decimals) work in computers
try printing (0.2+0.1) and you will get a similar result
that is also a reason why the meshes in workspace will look weird if they are very far from the origin point (0,0,0)
Floating Point Numbers - Computerphile
how floating point works

how about rounding the decimal instead?

number = tostring(number)
local splitnumber = number:split(".");
local Decimal = splitnumber[2];
if Decimal then
	local RoundedDecial = math.round(tonumber(Decimal));
	local NewNumber = tostring(splitnumber[1]).."."..tostring(RoundedDecial);
	number = tonumber(NewNumber);
end;

Correct me if I’m wrong, but doesn’t this script always do nothing? :sob:

The split number after the decimal point will always be an integer

As GE_0E stated, the anomaly you’re experiencing is a very normal thing. Luau’s numbers are double-precision floating-point numbers, which are stored using scientific notation (this is standard). There is an innate inaccuracy with this method of representation which cannot be avoided, as the binary number system cannot do better. Floating-point arithmetic can result in these tiny errors, and these errors will grow over time. Unfortunately, Luau does not offer any functions in the math library that can round to specific decimal places. The only functionality of this nature that you can find in the language is through string.format:

print(string.format("%.1f", 2000-1500.9)) --> 499.1
2 Likes

string.format() is probably the answer OP wanted, thanks for posting it! Here jesse, I made this OpRound function for you which works almost the same as using tonumber(string.format()) solution.

It automatically finds the precision to round to, unlike string.format(). You have to supply an operation function though.

local function RoundNearest(x, multiple) 
	return math.floor(x/multiple + multiple*0.5)*multiple
end

local function GetDecimalAsString(x)
	return tostring(x):split(".")[2] or ""
end

local function OpRound(a, b, operation)
	local splitA, splitB = GetDecimalAsString(a), GetDecimalAsString(b)
	
	local lenA, lenB = #splitA, #splitB
	
	local Significance = lenA > lenB and lenA or lenB
	
	local RoundToNearest = math.pow(10, -Significance)
	
	return RoundNearest(operation(a, b), RoundToNearest)
end


local operation = function(a, b)
	return a - b
end

local rounded = OpRound(2000, 1500.9, operation)

print(rounded) -- 499.1

thanks, i guess i learnt about floating point numbers along the way too lol

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.