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>


shihoAsked:
Who is Participating?
 
Michel PlungjanIT ExpertCommented:
function convert(val) {
   val = eval(val); // takes care of fractions
   val = val-0;
   if (in2mm) return round(val*25.4,2);    // in -> mm
   else       return round(val*0.03934,2);  // mm -> in
}


2 decimals now on all

Michel
0
 
Michel PlungjanIT ExpertCommented:
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
0
 
shihoAuthor Commented:
Excellente!

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

Cheers.

SK
0
 
MoondancerCommented:
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.

http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
http://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.