Solved

floating point precision in an HTML text field

Posted on 2001-09-10
1
194 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

821 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