#DaMiAnO Posted December 1, 2011 Posted December 1, 2011 Hello! I want "cut" the number in "tonumber". I have this code: tonumber(kmh) , but KM/h is displays, for example: 3.444444545488784848. I want only 3.44 KM/h. It's possible to do that?
Castillo Posted December 1, 2011 Posted December 1, 2011 You can use math.floor, but that will make it: 3 KM/h.
#DaMiAnO Posted December 1, 2011 Author Posted December 1, 2011 What to use "math.floor"? Wiki doesn't have infs for math.floor.
myonlake Posted December 1, 2011 Posted December 1, 2011 Wiki doesn't have everything, that's why you should check lua.org
50p Posted December 1, 2011 Posted December 1, 2011 Hello!I want "cut" the number in "tonumber". I have this code: tonumber(kmh) , but KM/h is displays, for example: 3.444444545488784848. I want only 3.44 KM/h. It's possible to do that? kmh = tonumber( string.format( "%.2f", 3.444444545488784848 ) ); -- kmh == 3.44
Bssol Posted December 1, 2011 Posted December 1, 2011 If you want to make it to the nearest integer number, you can use solidsnake's code. But if you want to make it with a few decimal numbers, you need to put this function to your script. function math.round(number, decimals, method) decimals = decimals or 0 local factor = 10 ^ decimals if (method == "ceil" or method == "floor") then return math[method](number * factor) / factor else return tonumber(("%."..decimals.."f"):format(number)) end end so, If you have a number of such 3.26897 math.round(3.26897, 2) --- 3.26 math.round(3.26897, 3) --- 3.268
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