473,322 Members | 1,540 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,322 software developers and data experts.

Dollar Format

chunk1978
224 100+
hey there...

i'm trying to format a 6 digit dollar amount to include a comma ($1,240) and a period ($1,240.50)... i have the period solved thru:

form.total.value = number.toFixed(2);

but how is it possible to add a comma in a dollar amount that is over one thousand dollars?
Feb 3 '07 #1
9 1952
acoder
16,027 Expert Mod 8TB
See this page. It should be what you want. They also have an advanced NumberFormat object which offers complex number formatting.
Feb 3 '07 #2
chunk1978
224 100+
this doesn't seem to work... what am i doing wrong?

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  3. <title>Untitled Document</title>
  4. <script type="text/javascript">
  5. function addCommas(nStr)
  6.     {
  7.     nStr += '';
  8.     x = nStr.split('.');
  9.     x1 = x[0];
  10.     x2 = x.length > 1 ? '.' + x[1] : '';
  11.     var rgx = /(\d+)(\d{3})/;
  12.     while (rgx.test(x1)) {
  13.         x1 = x1.replace(rgx, '$1' + ',' + '$2');
  14.     }
  15.     return x1 + x2;
  16. }
  17. </script>
  18. </head>
  19.  
  20. <body>
  21. <label>
  22. <input type="text" name="textfield" onBlur="addCommas(this.nStr);">
  23. </label>
  24. </body>
  25. </html>
  26.  
Feb 3 '07 #3
acoder
16,027 Expert Mod 8TB
It won't work because the input object has no attribute nStr. You have to pass the value:
Expand|Select|Wrap|Line Numbers
  1. onBlur="addCommas(this.value);"
Feb 3 '07 #4
chunk1978
224 100+
It won't work because the input object has no attribute nStr. You have to pass the value:
Expand|Select|Wrap|Line Numbers
  1. onBlur="addCommas(this.value);"
this doesn't work either...

Expand|Select|Wrap|Line Numbers
  1. <head>
  2. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  3. <title>Untitled Document</title>
  4. <script type="text/javascript">
  5. function addCommas(nStr)
  6.     {
  7.     nStr += '';
  8.     x = nStr.split('.');
  9.     x1 = x[0];
  10.     x2 = x.length > 1 ? '.' + x[1] : '';
  11.     var rgx = /(\d+)(\d{3})/;
  12.     while (rgx.test(x1)) {
  13.         x1 = x1.replace(rgx, '$1' + ',' + '$2');
  14.     }
  15.     return x1 + x2;
  16. }
  17. </script>
  18. </head>
  19.  
  20. <body>
  21. <label>
  22. <input type="text" name="textfield" onBlur="addCommas(this.value);">
  23. </label>
  24. </body>
  25. </html>
  26.  
i don't think i clearly understand yet when the event handeler (?i think that's what it's called) calls a functions.... like i usually match "();" with "()", and "(this.form);" with "(form)" and "(this.value);" with "(val)"... i'm sure i missed something along with way of learning javascript...
Feb 3 '07 #5
acoder
16,027 Expert Mod 8TB
You need to set the value too:
Expand|Select|Wrap|Line Numbers
  1. onBlur="this.value=addCommas(this.value);"
Although it shouldn't affect the input box display, you should include it within form tags.
Feb 4 '07 #6
chunk1978
224 100+
ok that worked... thanks...

one last question concerning this issue... is it possible to combine the AddCommas function to another one instead of using the onChange event handeler? the reason is because the number text fields are generated by select menus, not by actual typing input... i realize i could just add onChange event handlers to the select menus, but i think i'd prefer combine the two functions, to make the code tighter (i guess?)... plus it would be nice to learn :-)

i would like to combine the following two functions... and have the AddCommas function work for the:

A) form.invoicesubtotal.value
B) form.total2.value

Expand|Select|Wrap|Line Numbers
  1. function InvoiceSubtotalTaxTotal(form)
  2.     {
  3.     var a = (form.assignmentinvoice.value != '') ? eval(form.assignmentinvoice.value) : 0;
  4.     var b = (form.printinginvoice.value != '') ? eval(form.printinginvoice.value) : 0;
  5.     var invoicesubtotalVAR = a + b;
  6.     form.invoicesubtotal.value = '$' + invoicesubtotalVAR.toFixed(2) + ' CAD';
  7.     var SubTotal =  invoicesubtotalVAR;
  8.     var GST = SubTotal * 0.06;
  9.     form.gst.value = GST.toFixed(2);
  10.     var QST = (SubTotal + GST) * 0.075;
  11.     form.qst.value = QST.toFixed(2);
  12.     var Shipping = (form.shippinginvoice.value != '') ? eval(form.shippinginvoice.value) : 0;
  13.     form.shippinginvoice.value = Shipping.toFixed(2);
  14.     var TOTAL = SubTotal + GST + QST + Shipping;
  15.     form.total2.value = '$' + TOTAL.toFixed(2) + ' CAD';
  16. }
  17.  
  18. function addCommas(nStr)
  19.     {
  20.     nStr += '';
  21.     x = nStr.split('.');
  22.     x1 = x[0];
  23.     x2 = x.length > 1 ? '.' + x[1] : '';
  24.     var rgx = /(\d+)(\d{3})/;
  25.     while (rgx.test(x1)) {
  26.         x1 = x1.replace(rgx, '$1' + ',' + '$2');
  27.     }
  28.     return x1 + x2;
  29. }
  30.  
Feb 4 '07 #7
acoder
16,027 Expert Mod 8TB
Keep both functions separate (easier to read, better modularity, one task- one function, general functions can be used in other places too, etc.)

Call the addCommas function from within the InvoiceSubtotalTaxTotal function, e.g. on total2:
Expand|Select|Wrap|Line Numbers
  1. form.total2.value = '$' + addCommas(TOTAL.toFixed(2)) + ' CAD';
Feb 5 '07 #8
chunk1978
224 100+
Keep both functions separate (easier to read, better modularity, one task- one function, general functions can be used in other places too, etc.)

Call the addCommas function from within the InvoiceSubtotalTaxTotal function, e.g. on total2:
Expand|Select|Wrap|Line Numbers
  1. form.total2.value = '$' + addCommas(TOTAL.toFixed(2)) + ' CAD';
great! works perfectly... thanks again acoder :-)
Feb 5 '07 #9
acoder
16,027 Expert Mod 8TB
No problem, you're welcome.
Feb 5 '07 #10

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

Similar topics

9
by: bdog4 | last post by:
I want to validate a number to so that they can only input $2.00, 5.00, 7.00, 50.00 etc... I don't wan them to be able to put cents on the amount. It can either either be 2 or 2.00 Any ideas on...
3
by: mark.a.lemoine | last post by:
I need to be selecting records based on the value of a string-format currency value. Specifically, I have a table with a field of type varchar(50). Stored in this field is a dollar-formatted...
7
by: tshad | last post by:
I need to convert a number I get from my Sql table (where the value is a string) to a string and have it be a precision of 2 and I want to reverse the sign. I originally used: ...
17
by: Digital Puer | last post by:
I've inherited some code where the coder placed dollar signs in his preprocessor macros. What is the significance of the dollar signs ($) ? Example: #define ALLOCATE(task,pointer) \ { \...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.