/    Sign up×
Community /Pin to ProfileBookmark

question re: script to add commas to numbers

I found some code on this site and tweaked it a bit so add commas to numbers that I pass to the function. Can anyone tell me why this doesn’t work? I know the function is receiving numbers because I had built in an alert to be sure.

Chart.prototype.getComma = function(r) {

var w = r.split(“”).reverse().join(“”);

var q = “”;
for (var i=0; i<w.length; i++) {
q += w.charAt(i);
if ((i + 1) % 3 == 0 && i+1 != w.length) {
q += “,”;
}
}

q = q.split(“”).reverse().join(“”);
return q
}

This does work when I change the r.split(“”).reverse().join(“”); to “123456789”.split(“”).reverse().join(“”); but it returns the #’s 1-9 as I have them hard-coded in. Why won’t the variable approach work here?

Thanks in advance.

Tom

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@amdRocksJun 10.2003 — I saw you posted this question few times...Offer some money man...I am sure some of the volunteers will spend some time on your problem..:-)
Copy linkTweet thisAlerts:
@BrainDonorauthorJun 10.2003 — Well, I did have a similar post (had more to do with graphs though), but decided to head down that road myself...then I hit this speed-bump. Besides, this isn't for my personal use...it's for my company. I don't think that my offering their money to people would be appreciated. ?

Tom
Copy linkTweet thisAlerts:
@CharlesJun 10.2003 — [font=monospace]<script type="text/javascript">

<!--

String.prototype.reverse = function () {return this.split('').reverse().join('')};

Number.prototype.withCommas = function () {

if (isNaN (this)) return NaN.toString();

var l = Math.floor(Math.abs(this)).toString();

var r = Math.round((Math.abs(this) % 1) * 100).toString();

return [(this < 0 ? '-' : ''), (l.length > 4 ? l.reverse().match(/d{1,3}/g).join(',').reverse() : l),'.', (r < 10 ? '0' + r : r)].join('');

}

alert (Number(12345.65).withCommas());

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@BrainDonorauthorJun 10.2003 — Well, here's how it ended up...


Chart.prototype.getComma = function(r) {

var l = Math.floor(Math.abs(r)).toString();

l = l.split("").reverse().join("");

var q = "";

for (var i=0; i<l.length; i++) {

q += l.charAt(i);

if ((i + 1) % 3 == 0 && i+1 != l.length)

{q += ",";}

}

q = q.split("").reverse().join("");

return q

}

Thanks for your help guys...it is appreciated. ?

Tom
×

Success!

Help @BrainDonor 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 5.24,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...