Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

floating point precision in an HTML text field

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
myudt
Asked:
myudt
1 Solution
 
psogaaCommented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now