Looking for a way to display my score with decimals and commas when it gets too big ie. 1,994,132

Hi

I’m looking for a way to display my score with decimals and commas when it gets too big ie. 1,994,132 or 100,543 so it’s more readable to the user but can’t work it out, i’ve always been terrible at maths :s

Any help much appreciated :slight_smile:

Thanks

I’m using this one, although currency specific, got it from somewhere:

[lua]-- Examples returned: $1,$22, $123, $1,212, $22,423, $522,312, $1,321 MIL

local function numberFormat(num, places)

    

    local isNegative = false; if num < 0 then isNegative = true end

    local num = math.abs(num)

    local ret

    local placeValue = ("%%.%df"):format(places or 0)

    if not num then

        return 0

    elseif num >= 1000000000000 then

        ret = placeValue:format(num / 1000000000000) … " TRIL" – trillion

    elseif num >= 1000000000 then

        ret = placeValue:format(num / 1000000000) … " BIL" – billion

    elseif num >= 1000000 then

        ret = placeValue:format(num / 1000000) … " MIL" – million

    elseif num >= 1000 then

        ret = string.gsub(num, “^(-?%d+)(%d%d%d)”, ‘%1,%2’)

        – ret = placeValue:format(num / 1000) … “k” – thousand

    else

        ret = num – hundreds

    end

    

    local currency = “$”

    if isNegative then currency = “-$” end

    

    return currency … ret

end[/lua]

More: http://lua-users.org/wiki/FormattingNumbers

thanks a lot this is great

I’m using this one, although currency specific, got it from somewhere:

[lua]-- Examples returned: $1,$22, $123, $1,212, $22,423, $522,312, $1,321 MIL

local function numberFormat(num, places)

    

    local isNegative = false; if num < 0 then isNegative = true end

    local num = math.abs(num)

    local ret

    local placeValue = ("%%.%df"):format(places or 0)

    if not num then

        return 0

    elseif num >= 1000000000000 then

        ret = placeValue:format(num / 1000000000000) … " TRIL" – trillion

    elseif num >= 1000000000 then

        ret = placeValue:format(num / 1000000000) … " BIL" – billion

    elseif num >= 1000000 then

        ret = placeValue:format(num / 1000000) … " MIL" – million

    elseif num >= 1000 then

        ret = string.gsub(num, “^(-?%d+)(%d%d%d)”, ‘%1,%2’)

        – ret = placeValue:format(num / 1000) … “k” – thousand

    else

        ret = num – hundreds

    end

    

    local currency = “$”

    if isNegative then currency = “-$” end

    

    return currency … ret

end[/lua]

More: http://lua-users.org/wiki/FormattingNumbers

thanks a lot this is great