Solved

FORMAT DECIMAL PLACES AND PAD  0's

Posted on 2008-10-01
2
2,176 Views
Last Modified: 2010-04-21
Hi
I am using the following script's attached  to format decimal places and pad zeros.
Is there a way to combine both pieces of code. At present one will undo the other
What I need is if I enter 8 ,  On Change  It becomes 008.0000 or if I enter 89.9  On Change it becomes
089.9000


Decimal places          onchange="fix(this)" 
function fix(o){
 if( !isNaN(parseInt(o.value,10)) )
 {
    o.value = parseInt(o.value).toFixed(4);
 }
}
 
Pad zeros     onblur="padValue(this,3);"
function pad(num, count) {
 
var lenDiff = count - String(num).length;
 
var padding = "";
 
 
if (lenDiff > 0)
 
while (lenDiff--)
 
padding += "0";
 
 
return padding + num;
 
}
 
 
function padValue(el, count) {
 
var num = Number(el.value);
 
 
if ( isNaN(num) ) {
 
alert("Please only enter numbers");
 
} else {
 
el.value = pad(num, count);
 
}

Open in new window

0
Comment
Question by:Camnoc
[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
2 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 22612638
Use the following function like this:

onblur="formatField(this,4,8);"
function formatField(obj, decimals, len) {
   var n = Number(obj.value);
   if (!isNaN(n)) {
      var s = parseInt(n).toFixed(decimals);
      while (s.length < len) s = '0' + s;
      obj.value = s;
   } else {
      alert("Please only enter numbers");
   }
}

Open in new window

0
 

Author Closing Comment

by:Camnoc
ID: 31501886
Hi
Thanks very much. Much better code.

Thanks again
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…
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…

688 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