Welcome to WebmasterWorld Guest from 54.145.246.183 register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts, Pubcon Gold Sponsor 2015!
 Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

# JavaScript and AJAX Forum

 Tweet
Decimal to fraction conversion problem
Frank_N

Msg#: 4401391 posted 10:45 pm on Dec 25, 2011 (gmt 0)

Hi,

I'm using the code found here...
[webmasterworld.com...]

to convert decimals to fractions and it works very well... the problem I'm having is that when there are too many numbers after the decimal it crashes. I'm using this as an input onchange="inch.value = eval(this.value) * 0.03937" and would like to limit the numbers after the decimal to 6.

e.g: when entering 10 millimeters the output is 0.39370000000000005 so I'd like to only use 0.393700.

Hope it's clear, I have no programming experience what so ever so I apologize in advance if this is trivial.

Thanks,
Frank

birdbrain

Msg#: 4401391 posted 11:38 am on Dec 26, 2011 (gmt 0)

Hi there Frank_N,

and a warm welcome to these forums. ;)

You do not mention which was the code of your choice. :(

Here is my code - with the denominator limited to six decimal places...

 ``

``

birdbrain

Frank_N

Msg#: 4401391 posted 5:23 pm on Dec 27, 2011 (gmt 0)

Hi Birdbrain,
Thanks for the warm welcome and your help :)

I'm so new at this I'm not sure what you're asking for lol

But here is the code I'm using... hope this helps...

// Converts decimals to fractions
function decimalPlaces(inch) {
var decimalSeparator = '.',
tmp = inch.toString(),
idx = tmp.indexOf(decimalSeparator);
if (idx >= 0) {
return (tmp.length - idx - 1);
}
return 0;
}

function decfrac(DtF) {
var i,
j,
gcf,
whole = 0,
n = decimalPlaces(DtF), // the number of decimals
m = Math.pow(10,n), // 10^n
numerator = Math.round(DtF * m), // Important to round the result
negative = numerator < 0,
denominator = 1 * (numerator != 0 ? m : 1)
// Save some work if numerator is zero
if (numerator == 0) {
return "0";
}
// Handle negative values
if (negative) {
// Temporarily convert to positive
numerator *= -1; // negative * negative = positive
}
// Get the Greatest Common Factor
for (i = (numerator > denominator ? numerator: denominator); i > 0; i--) {
if ((numerator % i == 0) && (denominator % i == 0)) {
gcf = i;
break;
}
}
numerator = numerator / gcf;
denominator = denominator / gcf;
if (numerator >= denominator) {
whole = Math.floor(numerator / denominator);
numerator = numerator % denominator;
}
return (negative ? "-" : "") +
(whole > 0 ? whole + " " : "") +
(numerator > 0 ? numerator + "/" + denominator : "");
}

function fractional_part(input, output) {
var elIn = document.getElementById(input),
elOut = document.getElementById(output),
val;
// Some basic validation
if (elIn && elOut) {
val = elIn.value;
val = val.replace(/^\s+|\s+\$/g,""); // trim off whitespace

if (isNaN(val) || val.length == 0) {
}

else {
// Do the math
elOut.value = decfrac(val);
}
}

else {
} return false;
}

<form action="#" onsubmit="return fractional_part('inch','res');">
<fieldset>
<legend>Convert between inches and mm</legend>
<table>
<tr align="center" valign="middle">
<td>Inches</td>
<td>&nbsp;</td>
<td>Millimeters</td>
<td>&nbsp;</td>
</tr>
<form>
<tr align="center" valign="middle">
<td><input type=text size=12 name="res" value="1" id="res" onchange="return fractional_part('inch','res');" readonly>
<input type="text" name="inch" size="12" value="1.000" onchange="milli.value = 25.400 * eval(this.value);" onkeypress='validate(event)'></td>
<td>=</td>
<td><input type="text" name="milli" size="12" value="25.400" onchange="inch.value = eval(this.value) * 0.03937" onkeypress='validate(event)'> </td>
<td><input type="submit" value="Convert"></td>
<!--<td><input type="reset" value="&nbsp;Reset&nbsp;"></td>-->
<td>&nbsp;</td>
</tr>
</form>
</table>
</fieldset>
</form>

rocknbil

Msg#: 4401391 posted 6:02 pm on Dec 29, 2011 (gmt 0)

Some good reading in understanding the Floating Point Precision [webmasterworld.com] problem and some ways to deal with it.

 Global Options: top home search open messages active posts  Tweet

 Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX