Convert me please.



Hello,

Below form/script assembly is not working. All that's required is for the script to convert from mm. to inches and back. All other units are superfluous.

Can you help?

Thank you

Normbry

The thing:

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

<SCRIPT LANGUAGE="JavaScript">
<!-- Generic Unit Conversion Program
// Author    : Jonathan Weesner (jweesner@cyberstation.net)  21 Nov 95
// Copyright : You want it? Take it! ... but leave the Author line intact please!
function convertform(form){
    var firstvalue = 0;
    for (var i = 1; i <= form.count; i++) {
       // Find first non-blank entry
       if (form.elements[i].value != null && form.elements[i].value.length != 0) {
   if (i == 1 && form.elements[2].value != "") return false;
   firstvalue = form.elements[i].value / form.elements[i].factor;
   break;
       }
    }
    if (firstvalue == 0) {
       clearform(form);
       return false;
    }
    for (var i = 1; i <= form.count; i++)
       form.elements[i].value = formatvalue((firstvalue * form.elements[i].factor), form.rsize);
    return true;
}
function formatvalue(input, rsize) {
   var invalid = "**************************";
   var nines = "999999999999999999999999";
   var strin = "" + input;
   var fltin = parseFloat(strin);
   if (strin.length <= rsize) return strin;
   if (strin.indexOf("e") != -1 ||
       fltin > parseFloat(nines.substring(0,rsize)+".4"))
      return invalid.substring(0, rsize);
   var rounded = "" + (fltin + (fltin - parseFloat(strin.substring(0, rsize))));
   return rounded.substring(0, rsize);

}
<!-- done hiding from old browsers -->
</SCRIPT><CENTER>
<FORM>
<TABLE CELLSPACING="0" CELLPADDING="2" border="1">
<TR>
<Th>mm</Th><TD>=</TD>
<Th>inches</Th></TR><TR>
<Th><INPUT TYPE=TEXT NAME=val2 SIZE=4></Th><TD>&nbsp;</TD>
<Th><INPUT TYPE=TEXT NAME=val1 SIZE=4></Th>


</TR>
<TR><Th COLSPAN="3"><INPUT TYPE="button"  VALUE="Calculeren" onclick="convertform(this.form)"></Th>
</TR>
</TABLE>
</FORM>
<SCRIPT LANGUAGE="JavaScript">
<!-- Set conversion factors for each item in form. All
// factors must convert the first item to the current item.
// Be sure to use the correct form index. The first form is
// always index "0" and remaining forms are numbered in the
// order they appear in the document.
document.forms[0].count = 2;  // number of unit types
document.forms[0].rsize = 7;  // Rounding size, use same as SIZE
document.forms[0].val1.factor = 1;            // m to m.
document.forms[0].val2.factor = 0.003937007874;  // m to in.


<!-- done hiding from old browsers -->
</SCRIPT>


</BODY>
</HTML>
normbryAsked:
Who is Participating?
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:
<SCRIPT>
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
}
</SCRIPT>
<FORM onSubmit="return false"">
<INPUT TYPE="text" NAME="from" VALUE="" onChange="this.form.to.value=convert(this.value)">
<BR><INPUT 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>
0

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:
Mine does not format the decimals though - do you want it to do that?

Michel
0
normbryAuthor Commented:
I also get an end quote in the bottom area at "mm to inches".

Could you round it to 5 decimals for us? Is that possible?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Michel PlungjanIT ExpertCommented:
The end quote is the Inches sign

Change

if (in2mm) return (val*25.4) + 'mm';    // in -> mm
else       return (val*0.03934)+'"';  // mm -> in

to

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

(remove what is between the + and the ;)
to get rid of the quote and the mm

To round, add this:

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);
 }

and change to

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

making the script look like this:

<SCRIPT>
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>

0
normbryAuthor Commented:
Like a charm!

Thanks

N
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
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
JavaScript

From novice to tech pro — start learning today.