Link to home
Start Free TrialLog in
Avatar of shiho
shiho

asked on

Fractions to decimal conversion script wanted

Hello,

I would like for our clients to enter inch lengths in fraction format (i.e. "3/8") such that the output is given in decimal 'mm' format. And - if possible - the script should also be able to work backwards. That's to say: if one were to enter "9.5 mm", the thing should disgorge "3/8". I think, however, that this is pushing it, somewhat.

All mm. values can be rounded off to a single decimal.

Thank you


Here's what I got:

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>

<SCRIPT LANGUAGE="JavaScript">
//
var in2mm = true;
function convert(val) {
   val = val-0;
//   if (in2mm) return (val*25.4) + 'mm';    // in -> mm
//   else       return (val*0.03934)+'"';  // mm -> in
   if (in2mm) return round(val*25.4,5);    // in -> mm
   else       return round(val*0.03934,5);  // mm -> in
}
function round(number,X) {
 // rounds number to X decimal places, defaults to 2
     X = (!X ? 2 : X);
     return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}
// --></SCRIPT>
<FORM onSubmit="return false"">
<INPUT SIZE="5" TYPE="text" NAME="from" VALUE="" onChange="this.form.to.value=convert(this.value)">
<BR><INPUT SIZE="5" TYPE="text" NAME="to" VALUE="">
<BR><INPUT TYPE="radio" NAME="into" VALUE="" onClick="in2mm=true;this.form.to.value=convert(this.form.from.value)" CHECKED>in. to mm.
<INPUT TYPE="radio" NAME="into" VALUE="" onClick="in2mm=false;this.form.to.value=convert(this.form.from.value)">mm. to in.
</FORM>

</BODY>
</HTML>


ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry

  if (in2mm) return round(val*25.4,1);    // in -> mm
  else       return round(val*0.03934,1);  // mm -> in

1 decimal.

If you can give me a list of valid fractions for the product, I can make a look up table and return a fraction.

I would suggest having both values like this:

[ 9.5mm (3/8") ]

perhaps

Michel
Avatar of shiho
shiho

ASKER

Excellente!

We'll do the reverse in Phase II, yes?

Cheers.

SK
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed.  Also a question is posted there specific to these changes that apply to the experts here.  Also, I am including the link to our All Topics, since many new ones have recently been added.

https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
https://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange