473,387 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,387 software developers and data experts.

format currency function has problems

I found the following script somewhere so I can change values to money before
the form is printed. The page works for most people but not some. It worked
fine for me but I get the yellow tri-angle with an exclamation point in the
bottom left part of the window. When I told my IE to display a notification
about every error...it told me there was a problem with line 11.
What is wrong with this?

9 function formatCurrency(strValue)
10 {
11 strValue = strValue.toString().replace(/\$|\,/g,'');
12 dblValue = parseFloat(strValue);
13 blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
14 dblValue = Math.floor(dblValue*100+0.50000000001);
15 intCents = dblValue%100;
16 strCents = intCents.toString();
17 dblValue = Math.floor(dblValue/100).toString();
18 if(intCents<10)
19 strCents = "0" + strCents;
20 for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
21 dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
22 dblValue.substring(dblValue.length-(4*i+3));
23 return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

error message
Line 11
Char 2
Error 'undefined' is null or not an object
code 0
Jul 23 '05 #1
3 2095
On Tue, 19 Oct 2004 11:50:30 -0500, Abby Lee <ab*******@hotmail.com> wrote:
I found the following script somewhere [...]
I seriously suggest you put it back.
When I told my IE to display a notification about every error [...]
You don't need to do that. You could just double-click on the icon on the
status bar.
What is wrong with this?


Aside from it being an unreadable mess, and containing some dubious hacks?

Try:

/* n - Number to format (in pennies).
* c - Currency symbol to use (defaults to none).
* g - Grouping symbol (defaults to none).
*
* Outputs a number of the form cngnnngnnn.nn
*
* For example, toCurrency(142635.7, '£', ',') produces
* £1,426.36
*/
function toCurrency(n, c, g) {
var s = (0 > n) ? '-' : ''; n = Math.abs(n);
var m = String(Math.round(n));
var j, i = '', f; c = c || ''; g = g || '';

while(m.length < 3) {m = '0' + m;}
f = m.substring((j = m.length - 2));
while(j > 3) {
i = g + m.substring(j - 3, j) + i;
j -= 3;
}
i = m.substring(0, j) + i;
return s + c + i + '.' + f;
}

Note the instructions for the first parameter: the value should be in
pennies (cents). Currency-related calculations should be performed like
this anyway to reduce the change of errors. If you aren't using pennies
and you don't want to change your existing script, multiply by 100 when
passing the value.

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
"Michael Winter" <M.******@blueyonder.co.invalid> writes:
On Tue, 19 Oct 2004 11:50:30 -0500, Abby Lee <ab*******@hotmail.com> wrote:
I found the following script somewhere [...]


I seriously suggest you put it back.


Now that's funny!
thanks for your help.
Jul 23 '05 #3
JRS: In article <opsf4o6zuox13kvk@atlantis>, dated Tue, 19 Oct 2004
17:49:22, seen in news:comp.lang.javascript, Michael Winter <M.Winter@bl
ueyonder.co.invalid> posted :
On Tue, 19 Oct 2004 11:50:30 -0500, Abby Lee <ab*******@hotmail.com> wrote:
I found the following script somewhere [...]
I seriously suggest you put it back.


Pereant, inquit, qui ante nos nostra dixerunt.

Try:

/* n - Number to format (in pennies).
* c - Currency symbol to use (defaults to none).
* g - Grouping symbol (defaults to none).
or Thousands separator

ISTM that it should also have
* d - Decimal separator (defaults to '.').
... Note the instructions for the first parameter: the value should be in
pennies (cents).
...


Bring back the ha'penny and farthing!

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bill Stanard | last post by:
I have had no success using the format function as follows (the two lines of code run one after the other): 'displays a running total of lblAmtdue.Caption 'contents in txtTotal.Text...
4
by: Patrick J. Schouten | last post by:
I am new to ASP/VBScript and I am trying to format a texbox when a user exits it. In Visual Basic you could put the following in the Exit property of the textbox: textbox1 = Format(textbox1,...
2
by: Dalan | last post by:
This should not be an issue, but it is. I'm sure that someone knows what little piece of code is needed too persuade Access 97 to include a currency format for labels (Avery, mailing type). Have...
1
by: Mike MacSween | last post by:
This looks like a bug to me. I have an expression on a report: =Format(Sum((**)*(/)),"0.00") Score is byte PercentOfGrade is double PropDegree is single ModuleCats is byte
2
by: DD | last post by:
I have a UnionQry that is run from StatementsI and StatementP. the Due and Paid colunms are currency and show the $ however when i union the two the currency format has gone. Any help please...
1
by: Parveen | last post by:
This is a pretty simple issue to solve...but i can't seem to figure it out!! I want data in some columns to show as currency so I set the format of a column to currency through code by ...
6
by: KevinW | last post by:
I need to format a currency column in a datagrid. The line I have tried is: Me.DataGrid1.TableStyles(0).GridColumnStyles(3).ToString.Format("#,##0.00") It does not work. I can format the...
2
by: crferguson | last post by:
Hello all! I'm having the oddest issue trying to format a numeric string as currency without decimals. For instance... strSalary = "120000.56" strSalary = Format(strSalary, "$#,##0") 'this...
16
by: Alexio | last post by:
Am new to javascript. I need to format the currency in the as numbers are entered in textboxes. I can format the currency in the calculations and display in the totals but can not figure out how to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.