Solved

Format number in javascript

Posted on 2014-04-07
3
223 Views
Last Modified: 2014-04-22
I have some input boxes on the screen to allow the user to type in the numbers.  I aslo have an input box to store the Total of all numbers above.  The input box for the Total cannot be changed and I want to display it as $12,345.67.  I use FormatCurrency(txtTotal,2) and the total show up as I want.  But then when I change one of the input numbers, the calculation is not correct.  When I remove FormatCurrency(), it works again.  Is there a way to make the calculation and display work at the same time?

Thanks,
0
Comment
Question by:ttta83
3 Comments
 
LVL 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 500 total points
Comment Utility
I have never had luck with formatcurrancy and I simply add the $ sign as text and use css for formatting.

There may be a rounding issue.

I use
$<%=formatnumber(txtTotal,2)%> or 
<%
response.write "$" & formatnumber(txtTotal,2)
%>

Open in new window

If you are doing any type of division, you need to first do your rounding.  http://www.w3schools.com/vbscript/func_round.asp
response.write(Round(24.53278,2)) ' = 24.53
response.write(Round(24.539122,2),2) ' = 24.54

%>

Open in new window

Putting it together
txtTotal=24.586
txtTotal=round(txtTotal,2) ' = 24.59
response.write "$" & formatnumber(txtTotal,2) ' $24.59

Open in new window

0
 
LVL 9

Expert Comment

by:Sar1973
Comment Utility
Can you post the code of your format function?
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now