ID:1245286
 
Keywords: big, numbers
<u><b>num2CommaNum(number)</b></u>

Have you ever wanted to display big numbers clearly in your games? Then use this process.

<u><b>Example</b></u>

<dm>
world << "[num2CommaNum(1482428)]"

Outputs: 1,482,428

world << "[num2CommaNum(148242.8)]"

Outputs: 148,242.8

Code

// Author: Hiead | Editor: Zecronious
// Date: 25/04/2013
//-------------------

proc
num2commaNum(theNum, sigFig = 7) // default sigFig (1,000,000)

var/finalNum = num2text(theNum, sigFig)

// Start from the end, or from the decimal point
var/end = findtextEx(finalNum, ".") || length(finalNum) + 1

// Moving towards start of string, insert comma every 3 characters
for(var/pos = end - 3, pos > 1, pos -= 3)
finalNum = copytext(finalNum, 1, pos) + "," + copytext(finalNum, pos)

return finalNum
You're from the future! It's 4/25/2013, and this was created on 4/25/2013.
In response to Flysbad
Oh right, I'm from New Zealand which is further ahead than your time. I did this at 1AM, the morning of the 25th. At which point for the majority of the world we were all still in the 24th.

New Zealand is GMT +12, or GMT +13 when it's daylight savings. So we're the first country to experience each new day. Gives funny results to you laggers!
I have finally caught up to where you was when you made this!
In response to Flysbad
Haha
I just wanted to interject, as your function doesn't always work very well and it is sub-optimal in terms of performance.

The main problem with your method is it does not handle decimals at all; it just blindly sticks commas everywhere.

I dislike that you always force 15 significant digits; BYOND can probably handle around 20, and this causes outrageous expansion of decimals.

Here's a method that handles a decimal point, produces the same output otherwise, and allows the same customization for significant digits as num2text(). In my tests it also seems to be significantly faster.
proc
num2comma(N, SigFig=6)
. = num2text(N, SigFig)
// Start from the end, or from the decimal point
var/end = findtextEx(., ".") || length(.)+1
// Moving towards start of string, insert comma every 3 characters
for(var/pos=end-3, pos>1, pos-=3)
. = copytext(., 1, pos) + "," + copytext(., pos)


Here is the code I used to compare to yours:
mob/verb/Test()
usr << "Starting test."
var/x = 123456789
ASSERT(num2commaNum(x) == num2comma(x, 15)) // ASSERT that my output is the same as yours

sleep(0)
for(var/i in 1 to 10000)
num2commaNum(x)
num2comma(x, 15)

usr << "Test complete."

And the result:
-----
I think you did a fine job of identifying a problem and conjuring a solution, and I don't want you to think I'm trying to one-up you. I'm merely hoping to give you something to learn from, and I wanted to point out an oversight you made in not accounting for decimals. Where possible, always try to make your functions flexible (as in the SigFig parameter here), as it encourages re-use.

Lastly, I think you are one of the rare promising prospects of programmers that are bred from this site and I look forward to your progress and contributions in the future, so don't take my blunt criticisms harshly. Godspeed.
In response to Hiead
Hiead, this is really int2CommaInt. It's not float2CommaFloat. Byond is really quite relaxed about what type you're using so there's no inbuilt way to specify that. That's why I left it as it is.

About the significant figures, it's all about where this method is being called from and why I made it. I didn't want to design a really fast algorithm. I had a problem and I wanted to tick it off as soon as possible and it's not a proc that gets called very often so it makes no difference how fast it is. In the end there was one good option for me which was make something that works in a short amount of time. Still, the ultimate goal of coding is to write better code so I appreciate the help.

In the end your code is better, very cool actually. The way you fixed the problems are the same way I would've done 'if had more time' but thanks for doing it cause I didn't have time xD

One thing though. I think your code is better but it's also not as easy to understand so when I post it up above I'll change it to hopefully make it more readable. Still, great work and thanks for the help. Hope you don't mind me changing it above, I'll add you as the writer and edited by me. Don't think my changes will change efficiency, maybe just add some extra memory being used, like a few bytes.
In response to Hiead
Added the new code to the top post, checked it and it's working good. Just letting you know encase you want to check it yourself.