Formatting Decimal To 3 Places

Hello friends, quick question!

I need to format decimals to 3 places. How should I approach this?

For Example:

  • 0       ----> .000
  • .25    ----> .250
  • .1      ----> .100
  • .2983 —> .298
  • .2986 —> .299
  • 1       ----> 1.000

Thanks in advance!

string.format("%0.3f", value)

Rob

Thanks for the reply!

Right now, I have this code, which works

local avg = string.format("%.3f",player.AVG) if(string.sub(avg, 1,1) == "0") then --Remove leading 0, if any     avg = string.sub(avg, 2) end

But it seems a little complicated and messy.

Is there any way to get rid of the leading 0 too, only using string.format()?

Technically speaking, numbers smaller than 1, should have a 0. in front of it.  There isn’t an format I know of that won’t try and put a 0 there.  You’re way of removing it is probably the best way.

Rob

You’re right that 0s should usually be in front of numbers less than one, but in baseball statistics, the 0 is usually omitted!

http://espn.go.com/mlb/statistics

string.format("%0.3f", value)

Rob

Thanks for the reply!

Right now, I have this code, which works

local avg = string.format("%.3f",player.AVG) if(string.sub(avg, 1,1) == "0") then --Remove leading 0, if any     avg = string.sub(avg, 2) end

But it seems a little complicated and messy.

Is there any way to get rid of the leading 0 too, only using string.format()?

Technically speaking, numbers smaller than 1, should have a 0. in front of it.  There isn’t an format I know of that won’t try and put a 0 there.  You’re way of removing it is probably the best way.

Rob

You’re right that 0s should usually be in front of numbers less than one, but in baseball statistics, the 0 is usually omitted!

http://espn.go.com/mlb/statistics