Solved

floating point precision in an HTML text field

Posted on 2001-09-10
1
195 Views
Last Modified: 2008-03-03
At this URL
http://www.cadenzasoftware.com/sample/printing/form.htm
I need the price field to always show two decimal points precision.  As it is now, two decimal points are shown only when the page loads, because I have initialized the price field to 150.00.  Play with the fields and controls and you will see that the fractional part goes away.  All of the floating point values that I use in the script have two decimal places, even 0.00.  How do I maintain two decimal places in the price field?
0
Comment
Question by:myudt
1 Comment
 
LVL 2

Accepted Solution

by:
psogaa earned 100 total points
ID: 6470601
Use this function.
Then in your printing.js, replace the following line:

document.quote.price.value = price;

with:

document.quote.price.value = FormatNumber(price,2);


function FormatNumber(expr, decplaces) {
  var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
  while (str.length <= decplaces){
    str = "0" + str;
  }
  var decpoint = str.length - decplaces;
  return str.substring(0,decpoint) + "." + str.substring(decpoint, str.length);
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hide cell in a table 2 27
how can i get the sole odd and th sole even 2 33
Javascript 2 22
Search Item in Table 2 19
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

856 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