PtokaX forum

Archive => Archived 4.0 boards => Help with Lua 4 scripts => Topic started by: (!(Tys?kan)=) on 26 April, 2004, 17:40:41

Title: Numbers
Post by: (!(Tys?kan)=) on 26 April, 2004, 17:40:41
Hi, how can I'm convert 4.93802935 to 4.39...I'm thinking like's strfind(4.93802935,"(%x+.%x)")
Title:
Post by: NotRabidWombat on 26 April, 2004, 17:57:50
More efficient:
function Round(iNumber)
   assert(type(iNumber) == "number");
   return floor( iNumber + 0.5 );
end
Round is designed to round the first place after the decimal. So you would use:

local myNumber = 4.93802935;
myNumber = Round( mNumber * 100 ) / 100;

Just presenting concept here. Haven't actually tested.

Future functionality questions > http://www.lua.org/manual/4.0/manual.html

-NotRabidWombat
Title:
Post by: (!(Tys?kan)=) on 26 April, 2004, 18:09:39
10x u :D
Title:
Post by: Xico on 27 April, 2004, 00:35:29
what about:

   myNimber =  4.93802935
   myNimber = format("%0.2f", myNimber )

u will get 4.93
Title:
Post by: NotRabidWombat on 27 April, 2004, 02:14:09
Yes that will also work. However the return result is a string. I was assuming he wanted rounding rather than truncation.

-NotRabidWombat
Title:
Post by: Xico on 28 April, 2004, 02:28:29
local myNumber = 4.93802935
myNumber= format("%0.2f", myNumber)

you will get (and I typed it wrong) 4.94

local myNumber = 4.93328029
myNumber= format("%0.2f", myNumber)

you will get 4.93


and round() is not a Lua interface
Title:
Post by: NotRabidWombat on 28 April, 2004, 03:09:25
Ah cool. Did not know format would handle that.

"and round() is not a Lua interface"

I don't understand your point. It's a function that I just wrote.

-NotRabidWombat