[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# FORMAT DECIMAL PLACES AND PAD  0's

Posted on 2008-10-01
Medium Priority
2,179 Views
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);
}
}

var lenDiff = count - String(num).length;

if (lenDiff > 0)

while (lenDiff--)

}

var num = Number(el.value);

if ( isNaN(num) ) {

} else {

}
``````
0
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

LVL 29

Accepted Solution

Göran Andersson earned 2000 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 {
}
}
``````
0

Author Closing Comment

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

Thanks again
0

## Featured Post

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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 Month13 days, 14 hours left to enroll