View previous topic :: View next topic |
Author |
Message |
AmyGrrl Cheater
Reputation: 0
Joined: 15 Dec 2016 Posts: 31
|
Posted: Mon Feb 21, 2022 12:30 pm Post subject: How do you remove some decimals without rounding? |
|
|
How do you remove all decimals or even keep some decimals without rounding? Every option I've been able to find to remove decimals. Always rounds the values so then when I compare them. They are all end up being the same number.
TestBuffer1, TestBuffer2, TestBuffer3 = 0.41099548, 0.20549776, 0.20549774
If you take the above example and use math.floor and round to 1 decimal place they all end up being 0.5. So I can't compare them.
I just want to take the value 0.41099548 and then select to remove all decimals or keep some of them. So it would return 0 or 0.4 or 0.41 etc.
Thanks for any help given!
|
|
Back to top |
|
 |
Dark Byte Site Admin
Reputation: 470
Joined: 09 May 2003 Posts: 25791 Location: The netherlands
|
Posted: Mon Feb 21, 2022 12:54 pm Post subject: |
|
|
string.format("%.2f")
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
Back to top |
|
 |
AmyGrrl Cheater
Reputation: 0
Joined: 15 Dec 2016 Posts: 31
|
Posted: Mon Feb 21, 2022 1:10 pm Post subject: |
|
|
Thank You! I did actually figure out a way to do it after I had posted. So I did some tests to see which is better. Which you can see below. The first method seems to be more accurate. They way you suggested seems to do some rounding still.
Test = 0.41199548
print (Test)
print (" ")
print (math.floor(Test))
print (math.floor(Test * 10) / 10)
print (math.floor(Test * 100) / 100)
print (math.floor(Test * 1000) / 1000)
print (math.floor(Test * 10000) / 10000)
print (" ")
print (tonumber(string.format("%.0f", Test)))
print (tonumber(string.format("%.1f", Test)))
print (tonumber(string.format("%.2f", Test)))
print (tonumber(string.format("%.3f", Test)))
print (tonumber(string.format("%.4f", Test)))
The results
0.41199548
0
0.4
0.41
0.411
0.4119
0
0.4
0.41
0.412
0.412
|
|
Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4702
|
Posted: Mon Feb 21, 2022 1:59 pm Post subject: |
|
|
Convert it to a string and take substrings from that
Code: | local foo = tostring(math.pi)
for i = #foo, foo:find('.', 1, true), -1 do
print(foo:sub(1,i))
end |
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
Back to top |
|
 |
ByTransient Expert Cheater
Reputation: 5
Joined: 05 Sep 2020 Posts: 240
|
Posted: Mon Feb 21, 2022 6:15 pm Post subject: |
|
|
ParkourPenguin wrote: | Convert it to a string and take substrings from that
Code: | local foo = tostring(math.pi)
for i = #foo, foo:find('.', 1, true), -1 do
print(foo:sub(1,i))
end |
|
That's good @ParkourPenguin.
Code: | function fmt1(foo,len)
rst=""
for i = #foo, foo:find('.', 1, true), -1 do
if i==tonumber(len) then
rst=foo:sub(1,i)
end
end
return rst
end
a3=fmt1("0.41099548",3)
a4=fmt1("0.41099548",4)
a5=fmt1("0.41099548",5)
print("3: " .. a3)
print("4: " .. a4)
print("5: " .. a5) |
rst:
3: 0.4
4: 0.41
5: 0.410
|
|
Back to top |
|
 |
|