The number without commas

Posted by sergio_br on Mon, 07/18/2005 - 01:51Syndicated Download Counter

The new format of xml file brings the number wonderfully naked ( without commas, ready for calculations).

BUT a good web designer would get it "naked" with JUST one script instruction like:

number = number.replace("," , "");


The number is too big to be exibited without thousands separators.
I had to add commas/dots to make it a bit more readable.

Pardon me, but I think you made a mistake modifying the number format. The old way, with thousands separator was good both for displaying and calculating (just one line of code would remove the ",").

Now, in order to display, people have two choices:

Or they exibit this "confuse to read" number with 8 digits long
Or they RE-add the commas with a special function for it.

Please if you modify the number format, post a blog in the homepage, so that the counter users get informed.


Submitted by pchere on Tue, 07/26/2005 - 15:09.

More ways to add a Firefox Download Counter.

Submitted by Paeniteo on Mon, 07/25/2005 - 20:07.

You can add the commas back using only one line as well (in PHP):
$number = number_format($number);

Germans (for example) may want to use the following line to get dots:
$number = number_format($number, 0, '', '.');

Submitted by sergio_br on Wed, 03/02/2005 - 11:13.

Simpler than ever, just copy and paste in your page to formatt the download counter:

You call the function specifiying the type of separator desired: '.' or ',' or even ' ' (space).
The number goes in expr argument.

Here is the code:

<script type="text/javascript">

function thousands( expr, separator){
var i, j = 0, s = expr, formatted = "";
/* Sergio, http://sitedosergio.sitesbr.net */
if( s.indexOf('.') > -1 ||
s.indexOf(',') > -1){ return s;}

for( i = s.length; i >= 0; i--){
j++;
if (j % 3 == 0 && i > 0) formatted = separator + s.substring(i-1, i) + formatted; else
formatted = s.substring(i-1, i) + formatted;
}

return formatted;

}
/* End of function */
</script>

Ex of use:

var new_number = thousands( number, ",");

;)

Sergio_br

Towards 100 million FFs Celebration

Submitted by Dave Ruske on Thu, 07/21/2005 - 14:29.

Tjalling Kikkert sent me a solution which appears to have originated from this post by Shawn Milo:

function addCommas_skm(someNum){
while (someNum.match(/^\d\d{3}/)){
someNum = someNum.replace(/(\d)(\d{3}(\.|,|$))/, '$1,$2');
}
return someNum;
}

The separator desired goes between the $1 and the $2.

Unfortunately, I couldn't use this solution as I wanted to support Indian (12,34,56,789) and Japanese/Chinese (not sure how to insert it in this message, but it groups by 4 digits separated by ideographs) as well as other formats. Still, if grouping by threes is all you need, Shawn's solution is concise.

Submitted by Dave Ruske on Tue, 07/19/2005 - 18:13.

There's a bit of Javascript I'm using in my OS X widget that anyone's free to use as they wish; find it in the July 16, 2005 post at www.ruske.net.

And, er, let me know if you see anything silly in the code... I'm no Javascript wizard by any means!

Submitted by Kris Silver on Tue, 07/19/2005 - 14:53.

At best, regardless, it is very un-readable, and tacky looking. The comma's need to come back.

Submitted by Paeniteo on Mon, 07/18/2005 - 16:16.

While it may be more convenient to have the commas in place, they actually have nothing to do in the RSS feed.
The purpose of the feed is to transport a number and not some arbitrary string that might be suitable for some special display-purposes.
This mixes data and style, something that we are actually strive to remove in HTML (already seen the new XHTML2-spec?) - why would we want to have in back in RSS?

If you want commas as thousands-separators (which is locale-dependent, btw), the correct way would be to add them later via a formatting operation. It may be more than a single line, but you only have to write the code once.
Other ppl might like to only have commas after the million-digit, use dots (german locale) of do whatever kinds of calculation based on the *number*.

Submitted by thomashauk on Tue, 07/19/2005 - 16:39.

Finger spaces are correct in the UK, commas are used for listing numbers.