?
Solved

Format number in javascript

Posted on 2014-04-07
3
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 2000 total points
ID: 39985024
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
ID: 39985310
Can you post the code of your format function?
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

741 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