Drakath Posted June 5, 2014 Share Posted June 5, 2014 Lets say I have: local number = 5.123456 How can I output only the first two numbers? Link to comment
pa3ck Posted June 5, 2014 Share Posted June 5, 2014 https://wiki.multitheftauto.com/wiki/Math.round Please keep in mint it's a non-native MTA function, you will need to copy-paste the whole lot then use math.round to round up a number. Link to comment
Moderators Citizen Posted June 5, 2014 Moderators Share Posted June 5, 2014 And in the "manual" way: 1 - turn it into a string 2 - take the 4 first caracters of it (in "5,12" there are 4 caracters) 3 - turn the result back into a number local number = 5.123456 number = tonumber(string.sub(tostring(number), 1, 4)) outputChatBox(number) --outputs 5.12 Link to comment
Cadu12 Posted June 6, 2014 Share Posted June 6, 2014 And in the "manual" way:1 - turn it into a string 2 - take the 4 first caracters of it (in "5,12" there are 4 caracters) 3 - turn the result back into a number local number = 5.123456 number = tonumber(string.sub(tostring(number), 1, 4)) outputChatBox(number) --outputs 5.12 Code should be this: local number = 5.123456 number = tonumber(string.sub(tostring(number), 1, number < 0 and 5 or 4)) outputChatBox(...) -- 5.123456 outputs 5.12 outputChatBox(...) -- -5.123456 outputs -5.12 Link to comment
Moderators Citizen Posted June 6, 2014 Moderators Share Posted June 6, 2014 And in the "manual" way:1 - turn it into a string 2 - take the 4 first caracters of it (in "5,12" there are 4 caracters) 3 - turn the result back into a number local number = 5.123456 number = tonumber(string.sub(tostring(number), 1, 4)) outputChatBox(number) --outputs 5.12 Code should be this: local number = 5.123456 number = tonumber(string.sub(tostring(number), 1, number < 0 and 5 or 4)) outputChatBox(...) -- 5.123456 outputs 5.12 outputChatBox(...) -- -5.123456 outputs -5.12 Totally right, I didn't think about it. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now