What you're seeing is roundoff error.
.69, with the 9 repeating like that, is really close to .7, but it is not .7.
There is a simple way to get your desired 0.7, and that is to use the Lua rounding formula:
roundedNumber = math.floor(numberToRound + 0.5)
If you use this on .69, you may have to actually multiply the number by 10 then floor it.
Rounding .69 to 0.7:
local num = 0.69
function RoundDecimal(givenNum)
if tostring(givenNum):find(".") and (tostring(givenNum):find(".") - 1) == "0" then
local newNum = givenNum * 10
local flooredNum = math.floor(newNum + 0.5)
local finalNum = flooredNum / 10
return finalNum
end
end
print(RoundDecimal(num))
The output:
0.7
But what's going on with the NumberValue?
Well, the number you insert into a NumberValue is not taken as a literal double or float. This means that the compiler can throw a binary-stored number at you instead of the decimal inside of the NumberValue.
A similar dilemma occurs with the tostring() function. When tostring() converts a number to a string, it suspends the floating point of the number, making the number more vulnerable to being binary-stored instead of being literal (however this usually only occurs with decimals because they have floating points). If you run a loop like this...
for i = 100, 0, -0.1 do
wait(0.1)
print(tostring(i))
end
.. nothing appears to be off. Around 91.4, however, you'll start to see this:
91.4
91.3
91.20000000001
91.10000000001
91.00000000001
This is the type of thing that baffles programmers but is simple to work around.
7 comments
https://developer.roblox.com/en-us/api-reference/lua-docs/math
You can round the value either up, or down using math.floor or math.ceil. ^
Or you can use both, using if then statements checking if the value is >= 0.5, then round up, if it's < 0.5, round down.
no I mean I want the number value to actually say .7
https://scriptinghelpers.org/questions/31645/rounding-to-2-decimal-places, this will cut off anything off in the thousandths place and beyond.
Number Values are double-precision floating point number, or double. https://developer.roblox.com/en-us/api-reference/class/NumberValue
https://devforum.roblox.com/t/numbervalue-cant-be-0-4-it-changes-to-0-39999999999/30591