473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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
8 6843
waltapp
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
waltapp
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
cptuser
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
waltapp
31
I was writing the comma reply when you posted.
Nov 11 '08 #5
cptuser
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
waltapp
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
cptuser
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
16,027 Expert Mod 8TB
You may find this page useful.
Nov 12 '08 #9

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

Similar topics

2
by: Teis Draiby | last post by:
Does this already exist? Something like "\£" that would appear like a "," or "." dependent on the current locale settings. Examples: string stringValue = "1000\£54"...
4
by: SAN CAZIANO | last post by:
how can I create a function like this that let me insert only symbol (in ascii code the symbol is from 33 to 47 and from 58 to 64 and from 91 to 96 and from 123 to 127) function...
0
by: Kaja | last post by:
Hi, Compare validator throws error if I have thousand seperator in the double value. ControlToValidate: TextBox1 Operator: DataTypeCheck Type: Double Any help appreciated.
10
by: Mike9900 | last post by:
Hello, I need a regular expression to match a currency with its symbol, for example Pound66.99 must return 66.99 or Pound(66.99) or Pound-66.99 or -66.99Pound return -66.99 or any other...
2
by: Zytan | last post by:
This page http://blog.stevex.net/index.php/string-formatting-in-csharp/ shows: {0:0,0} but for single digit numbers, it prefixes a 0! Is there a way that works with single digit numbers? thanks...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
5
by: ewanfisher | last post by:
I'm trying to find some code that will turn: 100 -100 1000 -1,000 1000000 -1,000,000 -1000 --1,000 I know that can be done using a regular expression. In Perl I would do something like:
1
gsgurdeep
by: gsgurdeep | last post by:
Hi! I am developing a accounting application using VB.NET and Access2007. then i use the crystal report 9.0 for report. plz explain how to use the thousand seperator formula in number field. ...
1
by: Joza | last post by:
Hi! How to allow only numbers and only ONE decimal seperator in textbox? Thnx.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.