Jump to content

Tonumber.


#DaMiAnO

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...