/    Sign up×
Community /Pin to ProfileBookmark

I found this code on the web and was wondering if anybody could comment it so that I could understand how to integrate it into my site.

[CODE]function number_format(n) {
var arr=new Array(‘0’), z=0;
while (n>0)
{arr[z]=”+n%1000; n=Math.floor(n/1000); z++;}
arr=arr.reverse();
for (var z in arr) if (z>0) //padding zeros
while (arr[z].length<3) arr[z]=’0’+arr[z];
return arr.join();
}[/CODE]

Thanks for your time,
Josh

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinJan 23.2006 — [CODE]

function number_format(n) {
//create an array with 1 element: '0'
var arr=new Array('0');
//create a variable 'z' and assign it 0 (zero)
z=0;
//while 'n' is greater than 0 (zero)
while (n>0){
//array element[z] = '' (empty string) + the returned value (modulus)
// of n divided by 1000
arr[z]=''+n%1000;
//set n to the nearest integer less than or equal to n/1000
n=Math.floor(n/1000);
//increment z
z++;
}
//reverse the array
arr=arr.reverse();
//loop through the array using z as a counter
for (var z in arr)
//if z is greater than 0 (zero)
if (z>0) //padding zeros
//while the length of array element[z] is less than 3
while (arr[z].length<3)
//array element[z] = '0' (zero) + the value of array element[z]
arr[z]='0'+arr[z];

//join the array elements into a string and return that to the caller
return arr.join();
}

[/CODE]
Copy linkTweet thisAlerts:
@Some1_SomewhereauthorJan 23.2006 — Thanks for the quick reply. I'm not very familiar with arrays, or how to use them. The commenting will help out a lot.

All I really want to do is have a textbox on this page:

[URL=http://www.dcameraman.com/modules.php?name=Chance_Or_Not]http://www.dcameraman.com/modules.php?name=Chance_Or_Not[/URL]

format the number inside to #,###,###,###,###.

Thanks again,

Josh
Copy linkTweet thisAlerts:
@Some1_SomewhereauthorJan 24.2006 — I was able to figure out what to do and how to implement the code.

Thanks again.

The link above should work to see it.

Josh
Copy linkTweet thisAlerts:
@VladdyJan 24.2006 — Check this out for all your number formating needs:

www.vladdy.net/demos/printf.html
×

Success!

Help @Some1_Somewhere spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...