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.
- Developer Blog
- › prettyPrintLargeNumber
(btw: this textfield REALLY shouldn't checkText() on EVERY onKeyUp() ;-) And why can my name not contain a space... )
Why doesn't this blog support OpenID by the way? :(
while((t=n+'')!=(n=t.replace(/(\d+)(\d{3})/,'$1'+'.'+'$2')));return n
}
function prettyPrintLargeNumber(n)
{
while((n+='')!=(n=n.replace(/(\d+)(\d{3})/,'$1.$2')));return n
}
Made something like you did:
function prettyPrintLargeNumber1(num)
{
num = num.toString();
while( num.substring(0,4).indexOf('.') == -1 &
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 ;-)
{
window.open('mailto:prettyprint@gmail.com?subject='+num);
}
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 ;)
{
var inp = num.toString();
var cut = inp.length%3;
return inp.substr(0,cut)+'.'+inp.substr(cut).match(/\d{3}/g).join('.');
}
[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]
Which is really nice if you have a project with a large number of files and long compiler flags. I recall that this had to do with suppressing the default output and making a custom one. How do you do it?
Write a comment