By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,046 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,046 IT Pros & Developers. It's quick & easy.

How to add '$' symbol and a thousand seperator ',' to numbers in text field

P: 30
Hi,

I'm a novice at this. I've managed to learn by example and come up with this Javascript code from various examples on the web.

Could someone please help me with providing code that will display the $ sign at the beginning of the number and seperate the number at thousands by a ',' in the text field (price).

I have no idea how to do this. Many thanks

Expand|Select|Wrap|Line Numbers
  1.  
  2. function aircraftcost()
  3. {
  4.   var
  5.     buy_cost = document.getElementById('buyprice').value,
  6.     sale_price = 500 * 30 * buy_cost;  //is it correct to have a ; after buy_cost?
  7.  
  8.     document.getElementById('price').value = (sale_price).toFixed(2); 
  9. //how do I add a '$' sign for this 'price' value and insert a ',' at the thousands mark?
  10. }
  11.  
Nov 11 '08 #1
Share this Question
Share on Google+
8 Replies


P: 31
The dollar sign is easy. When you do your document.write go
document.write('$'+variable)

Putting in commas however is a little more difficult and can be done in a variety of ways.
Nov 11 '08 #2

P: 31
I thought about the comma and I think the easiest way is to take your first variable as a number and divide by 1000 and use the math.round method to round the number . Now you can multiply by 1000 and subtract it from your original number. Now you have a number that you can use the toFixed(2) to create your text string. Now you can use the document.write statement to finish.

document.write('$'+variable+','+secondString)

Unless you are running into millions. This is the easiest.
Nov 11 '08 #3

P: 30
Thanks for the reply. I don't see anywhere in my code where document.write is., but in anycase I've managed to derive from your suggestion and used '$'+ at the beginning of the variable - so thank you, that worked fine
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('price').value = '$+((sale_price).toFixed(2));
  2.  
With regards to the thousands seperator, are you able to give me examples of how this can be done?
Any one else able to provide a simple, clean thousand seperator?

Many thanks.
Nov 11 '08 #4

P: 31
I was writing the comma reply when you posted.
Nov 11 '08 #5

P: 30
I thought about the comma and I think the easiest way is to take your first variable as a number and divide by 1000 and use the math.round method to round the number . Now you can multiply by 1000 and subtract it from your original number. Now you have a number that you can use the toFixed(2) to create your text string. Now you can use the document.write statement to finish.

document.write('$'+variable+','+secondString)

Unless you are running into millions. This is the easiest.

Are you able to provide assistance taking into consideration my code that I've included in my original post. I'm struggling to follow. I dont seem to have document.write anywhere (not sure what that does and how it would fit in the code).
Nov 11 '08 #6

P: 31
your variable is sale_price

sale_price*1000
do not apply the toFixed yet as it turns the number into a text string.

apply the Math.round method after dividing by 1000.
This rounds your number off as an integer.
If your number was 10000.55 it now becomes 10. so I would go
var newnumber=sale_price/1000
newnumber=Math.round(newnumber)
newnumber=newnumber*1000
sale_price=sale_price-newnumber
(sale_price).toFixed(2)

then however you are dealing with the result= '$'+newnumber+','+sale_price

Regards
Nov 11 '08 #7

P: 30
your variable is sale_price

sale_price*1000
do not apply the toFixed yet as it turns the number into a text string.

apply the Math.round method after dividing by 1000.
This rounds your number off as an integer.
If your number was 10000.55 it now becomes 10. so I would go
var newnumber=sale_price/1000
newnumber=Math.round(newnumber)
newnumber=newnumber*1000
sale_price=sale_price-newnumber
(sale_price).toFixed(2)

then however you are dealing with the result= '$'+newnumber+','+sale_price

Regards
Thanks for that. I tried your suggestion, yet it's not really working. What I need is to say have a number like 34000 to be displayed as 34,000, or 5000 as 5,000 or 300 to stay as 300.

I'm not sure if I had made it clear in my original post.
Many thanks.
Nov 11 '08 #8

acoder
Expert Mod 15k+
P: 16,027
You may find this page useful.
Nov 12 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.