## Formatting a number [solved]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Crossing
Prole
Posts: 42
Joined: Sat Mar 21, 2015 3:37 pm

### Formatting a number [solved]

So i've been trying to figure out how to format a number.

i know how to do it in the following way: (using http://lua-users.org/wiki/FormattingNumbers)

1000000 = 1,000,000

However, I want it to be this way:

1000000 = 1m
1200000 = 1.2m
500000 = 500k

The article doesn't really go over how i would accomplish this? I'm assuming you could just count the 0's and put the corresponding letter with amount of numbers to be shown. (500k, 1m, 1.2m)

I feel like this wouldn't be really efficient?
Last edited by Crossing on Thu Oct 31, 2019 6:04 pm, edited 1 time in total.

raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

### Re: Formatting a number

That's pretty much how you do it. The alternative way is to divide the number by 1000 and increment the suffix if it's still bigger than 1000.

zorg
Party member
Posts: 2992
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Formatting a number

Although, "counting the zeros" can be done many ways; if you'd plan on always converting to strings then doing some pattern matching, that's wasteful and slow; math has you covered though, you just need the 10-based logarithm of a number to know how many digits it has; after that, you could treat small numbers (below some cutoff like 1000) without a suffix, then between that and 1000000 with the "k" suffix, then finally above that with the "M" suffix. You probably also want to cut the non-significant digits to some amount, say 3 digits for all magnitudes.

Code: Select all

local suffix = {='','','','k','k','k','M','M','M'} -- could be continued, or M replaced by mil if you prefer...
function dynamicFormat(x)
local magnitude = math.max(0, math.floor(math.log10(x))) -- no suffixes for values between 0 and 1, though that could also be implemented...
return string.format("%0.3f%s", x/magnitude, suffix[magnitude])
end

Something like that i guess.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

pgimeno
Party member
Posts: 2310
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Formatting a number

Well, it depends on how you want to deal with numbers like 12345678. Counting the zeros will not work in this case.

My version is similar to zorg's but uses 'g' for formatting instead of 'f', which automatically removes trailing zeros. Also, it divides by the correct number Code: Select all

local format
do
local letters = {'', 'k', 'm', 'b', 't', 'qd', 'qt'}
function format(number)
local log = math.max(math.log10(number), 0)
local pwr = math.floor(log / 3)
assert(pwr + 1 <= #letters, "Number too big - we don't have enough suffixes")
return ("%.3g%s"):format(number / 1000^pwr, letters[pwr + 1])
end
end

print(format(0)) -- 0
print(format(1)) -- 1
print(format(123)) -- 123
print(format(999)) -- 999
print(format(1000)) -- 1k
print(format(1234)) -- 1.23k
print(format(2345)) -- 2.35k, note it rounds
print(format(23456)) -- 23.5k
print(format(234567)) -- 235k
print(format(2345678)) -- 2.35m
print(format(23456789)) -- 23.5m
print(format(2345678901)) -- 2.35b
print(format(2345678901234)) -- 2.35t
print(format(234567890123456789)) -- 235qd
print(format(234567890123456789012)) -- 235qt
print(format(2345678901234567890123)) -- abort, "Number too big, we don't have enough suffixes"

(Edited to rename "letters" to "suffixes" and to extend the suffixes by replacing "q" with "qd" and adding "qt"; also to make the suffix check dynamic)

### Who is online

Users browsing this forum: No registered users and 30 guests