/    Sign up×
Community /Pin to ProfileBookmark

formatting numbers in javascript

How do I format a number in javascript? For example, placing colons (,) as a thousand’s separator to a given number?

i.e.: 39687521.00 into 39,687,521.00
: 2000000.00 into 2,000,000.00

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@CharlesJul 14.2004 — [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(1234567890).withCommas())

</script>[/font]
Copy linkTweet thisAlerts:
@pelegk1Dec 28.2004 — if i want to do something like :

1,034+1,100=i will recive 2 and not 2,134

what do ido in this case?

thnaks i nadvance

peleg
Copy linkTweet thisAlerts:
@CharlesDec 28.2004 — Having first put a stop to the harassment of Mordechai Vanunu, try something like:

[font=monospace]<script type="text/javascript">

Number.fromCommas = function (s) {return Number (String (s).replace (/,/g, ''))}

alert (Number.fromCommas('1,034') + Number.fromCommas('1,100'))

</script>[/font]
Copy linkTweet thisAlerts:
@pelegk1Dec 28.2004 — first thanks alot!!!

second can you explain please what do u do execlly here :

String (s).replace (/,/g, '')

what the /g ??? ?

and third Having first put a stop to the harassment of Mordechai Vanunu[/QUOTE]
we already talk about it?
Copy linkTweet thisAlerts:
@JuuitchanDec 29.2004 — No. Your code is bad. What does 1.999 round to?
Copy linkTweet thisAlerts:
@JuuitchanDec 29.2004 — And also try -0.003 with your code to see another bug.

Try this instead: multiply the number by 100, THEN round to an integer, and only THEN convert to a string and drop the punctuation in the right places.
Copy linkTweet thisAlerts:
@JuuitchanDec 29.2004 — And if the string you get from the rounded number has less than 3 characters, you need to zero-pad it.
Copy linkTweet thisAlerts:
@pelegk1Dec 30.2004 — can i please write a correct code?
×

Success!

Help @pbarrero 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.17,
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,
)...