Formatting numerical output

taff's picture

They have: 956 posts

Joined: Jun 2001

How do I go about applying formatting to the result of a numerical calculation in a Javascript form?

ie - I want 150000 to display as $150,000

The $ is easy enough, I can apply that outside the output field but how could I get the comma in there?

.....

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

http://javascript.internet.com/forms/currency-format.html

Whoo! Google rocks. Man. That wasn't the top result, but it was the fourth unique site for javascript+money -- it would probably be first for javascript+currency.

Anyway, lol, hope that helps.

taff's picture

They have: 956 posts

Joined: Jun 2001

ooh boy. That's gonna be tough for a scripting bonehead like me to graft onto the existing script. I've got about a dozen results to apply this to Sad

Thanks for finding it though. I thought that there might be a simple property that I wasn't aware of.

.....

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

It's a function, so you would add it around the other functions.

If your result is dosomething(input,input,input), then you would do this:

currencyfunction(dosomething(input,input,input))

Of course, it's a little bit more complex than this, but not too much. Smiling Post a link to a sample of your code and I'm sure that someone (maybe even me!) can help you integrate it.

taff's picture

They have: 956 posts

Joined: Jun 2001

Trial and error is a wonderful thing! Smiling

I got it to work - thanks Suzanne.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Yay! Well done, then. Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.