Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


How do you remove some decimals without rounding?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
AmyGrrl
Cheater
Reputation: 0

Joined: 15 Dec 2016
Posts: 31

PostPosted: Mon Feb 21, 2022 12:30 pm    Post subject: How do you remove some decimals without rounding? Reply with quote

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
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 470

Joined: 09 May 2003
Posts: 25791
Location: The netherlands

PostPosted: Mon Feb 21, 2022 12:54 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
AmyGrrl
Cheater
Reputation: 0

Joined: 15 Dec 2016
Posts: 31

PostPosted: Mon Feb 21, 2022 1:10 pm    Post subject: Reply with quote

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
View user's profile Send private message
ParkourPenguin
I post too much
Reputation: 152

Joined: 06 Jul 2014
Posts: 4702

PostPosted: Mon Feb 21, 2022 1:59 pm    Post subject: Reply with quote

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
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Mon Feb 21, 2022 6:15 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites