Forum Moderators: open
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="language" content="english">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title></title>
<style type="text/css">
label {
display:block;
margin-bottom:10px;
}
#fraction {
margin-top:10px;
}
</style>
<script type="text/javascript">
function decfrac(DtF) {
dec=DtF;
if((isNaN(dec))||(dec=='0.')){
alert('please insert some numbers in the box !');
df.reset();
return;
}
temp=dec;
dec=dec.toString().split('.')[1];
frac=(Math.pow(10,dec.length)/parseFloat(dec));
if(frac.toFixed(6)!=frac){
temp1=frac=frac.toFixed(6);
temp1=parseFloat(temp1);
}
if(temp1==frac){
frac=temp1;
}
obj.firstChild.nodeValue='the decimal='+temp+', the equivalent fraction=1/'+frac;
}
function init(){
var temp1;
df=document.forms[0];
obj=document.getElementById('fraction');
df[1].onclick=function() {
decfrac(document.forms[0][0].value);
}
df[2].onclick=function() {
obj.firstChild.nodeValue='';
}
}
window.addEventListener?
window.addEventListener('load',init,false):
window.attachEvent('onload',init);
</script>
</head>
<body>
<form action="#">
<div>
<label>decimal : <input type="text" value='0.'></label>
<input type="button" value="convert to fraction">
<input type="reset" value="clear result">
</div>
</form>
<div id="fraction"> </div>
</body>
</html>