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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.