Fractions to decimal conversion script wanted


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:


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="">
<BR><INPUT SIZE="5" TYPE="text" NAME="to" VALUE="">
<BR><INPUT TYPE="radio" NAME="into" VALUE="" onClick="in2mm=true;" CHECKED>in. to mm.
<INPUT TYPE="radio" NAME="into" VALUE="" onClick="in2mm=false;">mm. to in.


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 PlungjanIT ExpertCommented:

  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") ]


shihoAuthor Commented:

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


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.
Thank you,
Moderator @ Experts Exchange
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.