472,959 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,959 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 2075
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.