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 -
-
function aircraftcost()
-
{
-
var
-
buy_cost = document.getElementById('buyprice').value,
-
sale_price = 500 * 30 * buy_cost; //is it correct to have a ; after buy_cost?
-
-
document.getElementById('price').value = (sale_price).toFixed(2);
-
//how do I add a '$' sign for this 'price' value and insert a ',' at the thousands mark?
-
}
-
8 6843
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.
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.
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 -
document.getElementById('price').value = '$+((sale_price).toFixed(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.
I was writing the comma reply when you posted.
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).
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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"...
|
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...
|
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.
|
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...
|
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...
|
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)...
|
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:
|
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.
...
|
by: Joza |
last post by:
Hi!
How to allow only numbers and only ONE decimal seperator
in textbox?
Thnx.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| | |