Solved

# floating point precision in an HTML text field

Posted on 2001-09-10
197 Views
At this URL
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
Question by:myudt
[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
1 Comment

LVL 2

Accepted Solution

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

Question has a verified solution.

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

### Suggested Solutions

How to pass values to HTML-5 attribute dynamically? 24 69
Read text on Table 7 49
Make New Browser Window Fully Functional 7 36
Jquery syntax 12 30
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…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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
Course of the Month3 days, 6 hours left to enroll