prettyPrintLargeNumber

Xopus is hiring!

The last few months have been extremely busy. We are working with many clients and partners to enhance their integrations. There is always a steady stream of support and forum questions. Development of the Xopus 4 platform uses a lot of our resources. And last but certainly not least Mark Wubben is following his dream to live and work in Copenhagen.

So we need more people! We would like to extend both our core development and integration teams. Our core developers write most of their code in Javascript and XSLT, so familiarity with those languages is convenient but not required. We think good developers easily learn new languages. As a matter of fact, several of our developers have no background in webdevelopment at all, but in Java, C++ or Haskell.

Members of the integration team work closely together with our clients and partners to find the best way to implement Xopus. This requires a lot of creativity and people skills. A lot of the work is done from our offices, but sometimes meeting face to face works better. Technologies regularly used include: Javascript, XML, XSLT, XSD, HTML and CSS. Familiarity is convenient but not required. Besides speaking Nederlands and English, being able to understand and speak Français and/or Deutsch can be very helpful when working with our international partners.

To reach a broad audience we will publish the following challenge in a Dutch national newspaper (Metro):

function prettyPrintLargeNumber(num) 
{
  var out = "";
  var inp = num.toString();
  for (var i = inp.length - 1, j = 0; i >= 0; i--, j++)  {    if (j % 3 == 0 && i != inp.length - 1)
  out = "." + out;
  out = inp.charAt(i) + out; }  
return out;
}

The challenge is to write a shorter, faster and prettier version of this function and send your entry to [challenge at xopus.com]. Following good Q42/Xopus tradition, the best entry will be rewarded with a nice single malt (in addition to the job offer).

We have had a lot of fun and discussions over several iterations of rewriting this piece of code. We hope you will enjoy it too!

Update: the deadline for competing entries is friday March 14th.

Modified: February 21st 2008
By: Laurens van den Oever

willemmulder
anonymous user
March 4th 2008
ah, typed that function from the newspaper myself. If I only Googled the function name ;-) It's just here to grab :)

(btw: this textfield REALLY shouldn't checkText() on EVERY onKeyUp() ;-) And why can my name not contain a space... )
Voila
anonymous user
March 5th 2008
prettyPrintLargeNumber = function (n) { n+=''; while(/(\d+)(\d{3})/.test(n)){n=n.replace(/(\d+)(\d{3})/,'$1.$2');} return n; }
bgeron
anonymous user
March 5th 2008
willemmulder: probably because "name" is like "username", you can couple a password to it and log in and stuff (see box on top-right)

Why doesn't this blog support OpenID by the way? :(
crisp
anonymous user
March 5th 2008
function prettyPrintLargeNumber(n){
while((t=n+'')!=(n=t.replace(/(\d+)(\d{3})/,'$1'+'.'+'$2')));return n
}
crisp
anonymous user
March 5th 2008
and even shorter:

function prettyPrintLargeNumber(n)
{
while((n+='')!=(n=n.replace(/(\d+)(\d{3})/,'$1.$2')));return n
}
willemmulder
anonymous user
March 7th 2008
@Crisp:
Made something like you did:
function prettyPrintLargeNumber1(num)
{
num = num.toString();
while( num.substring(0,4).indexOf('.') == -1 && num.length > 3) (num=num.replace(/([0-9]+)([0-9]{3})/,'$1.$2') );
return num;
}

but wasn't really satisfied with the long condition and over-the-top regexp. Then changed it to a for loop, and worked with substring instead of regexp.
Like:
function prettyPrintLargeNumber2(num) {
var inp = num.toString(), out="";
for(i=inp.length;i>0;i-=3) {
out = inp.substring((i-3),i) + "." + out;
}
return out.substr(0,out.length-1);
}

Your solution of checking the stringed n against a replaced one, is smart however :-) And as it seems the regexp is not too much over the top. It's faster than I expected.

btw: Why not just do n = n.toString(); (or n+=''; as you wish) once, then check against n in the while loop, instead of the longer (n+='') every time?
Like:
function prettyPrintLargeNumber(n)
{
n=n.toString();
while(n!=(n=n.replace(/(\d+)(\d{3})/,'$1.$2')));return n
}
Saves an operation every loop ;-)
Richard
anonymous user
March 12th 2008
function prettyPrintLargeNumber(num)
{
window.open('mailto:prettyprint@gmail.com?subject='+num);

}
Martijn
anonymous user
June 26th 2008
Hi Laurens,

I was browsing the site saw the contest ( Wich is expired ), how about some delphi code ;)

function PrettyPrintLargeNumber(Amount: Double): string;
begin
result := FormatFloat('#00,00', Amount);
end;

Maybe i will mail you a demo of my editor this weekend.

Greetz,
Martijn, Almost the Husband of Sandra ;)
ZishanZulfiqar
anonymous user
October 7th 2008
function prettyPrintLargeNumber(num)
{
var inp = num.toString();
var cut = inp.length%3;
return inp.substr(0,cut)+'.'+inp.substr(cut).match(/\d{3}/g).join('.');
}
RobbertBroersma
anonymous user
November 1st 2008
Haha, I think I like Richard's one most :D
Timvanoostrom
anonymous user
November 12th 2008
I agree, a truly creative solution :-)
PaulKlinkenberg
anonymous user
January 4th 2009
Way late, but had nothing to do ;-)
[code]
function prettyPrintLargeNumber(num)
{
num = String(num);
var prepend = num.length % 3 || 3;
return num.substr(0,prepend)
+ num.substr(prepend).replace(/([0-9]{3})/g, ".$1");
}
/* better one, which handles floating numbers:
function prettyPrintLargeNumber(num)
{
numparts = String(num).split('.');
var prepend = numparts[0].length % 3 || 3;
return numparts[0].substr(0,prepend)
+ numparts[0].substr(prepend).replace(/([0-9]{3})/g, ".$1")
+ (numparts[1] ? ','+numparts[1] : '');
}
*/
[code]