#DaMiAnO Posted December 1, 2011 Share 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? Link to comment
Castillo Posted December 1, 2011 Share Posted December 1, 2011 You can use math.floor, but that will make it: 3 KM/h. Link to comment
#DaMiAnO Posted December 1, 2011 Author Share Posted December 1, 2011 What to use "math.floor"? Wiki doesn't have infs for math.floor. Link to comment
myonlake Posted December 1, 2011 Share Posted December 1, 2011 Wiki doesn't have everything, that's why you should check lua.org Link to comment
Castillo Posted December 1, 2011 Share Posted December 1, 2011 kmh = math.floor(tonumber(kmh)) Link to comment
50p Posted December 1, 2011 Share 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 Link to comment
Bssol Posted December 1, 2011 Share 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 Link to comment
#DaMiAnO Posted December 2, 2011 Author Share Posted December 2, 2011 Thanks for help! 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