Round A Number To The First Decimal

Hi,

I would like to round some values.

Example : 5.5324343434   >>  5.5

I don’t want to round to the nearest integer (floor or ceil math function)

What I want to do, is only round to the first decimal like on these examples :

5.34343434 >> 5.3

2.9877939 >> 2.9

8.583002002 >> 8.5

Thank you for your help

Olivier

Simple enough:

[lua]

local function roundToFirstDecimal(t)

    return math.round(t*10)*0.1

end

[/lua]

C

Thank you Caleb :)

So simple!

Olivier

Simple enough:

[lua]

local function roundToFirstDecimal(t)

    return math.round(t*10)*0.1

end

[/lua]

C

Thank you Caleb :)

So simple!

Olivier

I’d like to also say thanks Caleb.  You just made a small portion of my project a bit easier and I’ll take every bit I can get.

I’d like to also say thanks Caleb.  You just made a small portion of my project a bit easier and I’ll take every bit I can get.

Thanks Caleb… that helped me!

A more generalized round function…

local function roundToNthDecimal(num, n)   local mult = 10^(n or 0)   return math.floor(num \* mult + 0.5) / mult end

 

Stash, I use that too.

function RoundNumber(num, idp)

    local mult = 10^(idp or 0)

    return math.floor(num * mult + 0.5) / mult

end

Thanks Caleb… that helped me!

A more generalized round function…

local function roundToNthDecimal(num, n)   local mult = 10^(n or 0)   return math.floor(num \* mult + 0.5) / mult end

 

Stash, I use that too.

function RoundNumber(num, idp)

    local mult = 10^(idp or 0)

    return math.floor(num * mult + 0.5) / mult

end