473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help needed for the weight conversion script

JS
I've managed to create a simple script to convert between metric and
imperial. It works for CMS to INCHES and vice versa but not for KGS to
STONES/POUNDS. Can anyone shed any light on this?

Here's my code (CMS and INCHES which is working):

<input name="CM" type="text" VALUE="" size="3" maxlength="3"
onChange="eval('IN.value = ' + this.form.IN_expr.value)">
cms <em>or</em>
<input name="IN" type="text" VALUE="" size="3" maxlength="3"
onChange="eval('CM.value = ' + this.form.CM_expr.value)">
inches<INPUT TYPE="hidden" NAME="CM_expr" VALUE="(Math.round(2.54 *
IN.value))">
<INPUT TYPE="hidden" NAME="IN_expr" VALUE="(Math.round(CM.value /
2.54))">

Here's my code for KGS to STONES/POUNDS (not working):

<input name="KGM" type="text" VALUE="" size="3" maxlength="3"
onChange="eval('ST.value = ' + this.form.ST_expr.value);
eval('PNDS.value = ' + this.form.PNDS_expr.value)">
KGM <em>or</em>
<input name="ST" type="text" VALUE="" size="3" maxlength="3"
onChange="eval('KGM.value = ' + this.form.KGM_expr.value)">St
<input name="PNDS" type="text" VALUE="" size="3" maxlength="3"
onChange="eval('KGM.value = ' + this.form.KGM_expr.value)">
lbs
<INPUT TYPE="hidden" NAME="KGM_expr" VALUE="(Math.round((ST.value * 14)
+ PNDS.value) * 0.4536)">
<INPUT TYPE="hidden" NAME="ST_expr" VALUE="(Math.round(KGM.value /
0.4536) / 14)"><INPUT TYPE="hidden" NAME="PNDS_expr"
VALUE="(Math.round(KGM.value / 0.4536) - ST.value)">

Firstly, I only want an integer in the result but am getting a decimal
to about 6 places.

Secondly, it appears that the + in (Math.round((ST.value * 14) +
PNDS.value) * 0.4536) is appending the PNDS.value to the ST.value * 14
so if PNDS.value is 0 and ST.value is 12 it calculates using 120.

I've spent a good few hours trying to get it to work or find the
suitable code but just can't find it.

Many thanks in anticipation.

Jul 23 '05 #1
8 2500
"JS" <go****@stanton.uk.net> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I've managed to create a simple script to convert between metric and
imperial. It works for CMS to INCHES and vice versa but not for KGS to
STONES/POUNDS. Can anyone shed any light on this?


[snip]

This link may already do what you want; look at the code behind.

http://www.convert-me.com/en/convert/weight

Here are some other calculator links:

http://www.chamberline.com/index.taf?_function=WebTools

http://www.wsdot.wa.gov/Metrics/factors.htm
Jul 23 '05 #2
JS
Thanks for the links. I think I've already found those sites on my
travels and they seemed a lot more complicated than I actually need. I
could really do with someone who's a lot better at Javascript to look
at my syntax and tell me where it's wrong.

Thanks again, much appreciated.

Jul 23 '05 #3
"JS" <go****@stanton.uk.net> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Thanks for the links. I think I've already found those sites on my
travels and they seemed a lot more complicated than I actually need. I
could really do with someone who's a lot better at Javascript to look
at my syntax and tell me where it's wrong.

Thanks again, much appreciated.


How about:

<html>
<head>
<title>titles.htm</title>
<script type="text/javascript">
function titles() {
var what = "";
for (var i=0; i<44; i++) {
what += " " + unescape("%A0");
}
document.title += what;
}
</script>
</head>
<body onload="titles()">
</body>
</html>
Jul 23 '05 #4
JS wrote:
I've managed to create a simple script to convert between metric and
imperial. It works for CMS to INCHES and vice versa but not for KGS to
STONES/POUNDS. Can anyone shed any light on this?

Here's my code (CMS and INCHES which is working):

<input name="CM" type="text" VALUE="" size="3" maxlength="3"
onChange="eval('IN.value = ' + this.form.IN_expr.value)">
cms <em>or</em>
The correct abbreviation for centimetre is "cm" and the correct
abbreviation for kilograms is "kg".
<input name="IN" type="text" VALUE="" size="3" maxlength="3"
onChange="eval('CM.value = ' + this.form.CM_expr.value)">
inches<INPUT TYPE="hidden" NAME="CM_expr" VALUE="(Math.round(2.54 *
IN.value))">
<INPUT TYPE="hidden" NAME="IN_expr" VALUE="(Math.round(CM.value /
2.54))">
Almost any use of 'eval' is wrong or not required, see below.

Here's my code for KGS to STONES/POUNDS (not working):
[...] <INPUT TYPE="hidden" NAME="ST_expr" VALUE="(Math.round(KGM.value /
0.4536) / 14)"><INPUT TYPE="hidden" NAME="PNDS_expr"
VALUE="(Math.round(KGM.value / 0.4536) - ST.value)">

Firstly, I only want an integer in the result but am getting a decimal
to about 6 places.
That is because in the expression:

(Math.round(KGM.value / 0.4536) / 14)

Math.round applies to KGM.value/0.4536. You then divide by 14 to
create your decimal. Fixing syntax and guessing what you
intended:

Math.round((this.form.KGM.value/0.4536)/14)

Secondly, it appears that the + in (Math.round((ST.value * 14) +
PNDS.value) * 0.4536) is appending the PNDS.value to the ST.value * 14
so if PNDS.value is 0 and ST.value is 12 it calculates using 120.


Values from inputs are always treated as strings initially so
you must force them to become numbers if that is what is
required.

Your use of "ST.value * 14" forces ST.value to be a number, but
PNDS.value is a string, so it's concatenated, not added. To
force it to be a number add a '+' as follows:

...((this.form.ST.value*14 + +this.form.PNDS.value)*0.4536)

You can do other tricks too (multiply by 1, subtract instead of
add, etc.), but this way makes it obvious what you are doing.

Below is a different version that does away with 'eval' and the
hidden inputs, but beware that you don't do any validation of
input. There are also issues with the precision of JavaScript
mathematics, you should read here to get more information about
doing maths with javascript:

<URL:http://www.merlyn.demon.co.uk/js-maths.htm>
<form action="">
<table>
<tr>
<td colspan="2">
All values rounded to nearest whole number
</td>
</tr><tr>
<td>
<input name="CM" type="text" VALUE="0" size="3"
maxlength="3" onChange="
// 1 inch = 2.54 cm
this.form.IN.value = Math.round(this.value/2.54);
"> cm <em>converts to</em>

</td><td>

<input name="IN" type="text" VALUE="0" size="3"
maxlength="3" onChange="
// 1 inch = 2.54 cm
this.form.CM.value = Math.round(this.value*2.54);
"> inches

</td>
</tr><tr>
<td>

<input name="KG" type="text" VALUE="0" size="3"
maxlength="3" onChange="
// 1 kg = 2.20462262 lbs
var x = Math.round(this.value*2.2046226);
// 14 lbs = 1 stone
this.form.ST.value = Math.floor(x/14);
this.form.LBS.value = x%14;
"> kg <em>converts to</em>

</td><td>

<input name="ST" type="text" VALUE="0" size="3"
maxlength="3" onChange="
this.form.KG.value = Math.round((this.value*14
+ +this.form.LBS.value)/2.2046226);
">St
<input name="LBS" type="text" VALUE="0" size="3"
maxlength="3" onChange="
this.form.KG.value = Math.round((this.form.ST.value*14
+ +this.value)/2.2046226);
">lbs</p>

</td>
</tr><tr>
<td colspan="2">
<input type="reset" onclick="if(this.blur) this.blur();">
</td>
</tr>
</table>
</form>
--
Rob
Jul 23 '05 #5
JS
Hi Rob

Fantastic reply. I shall go through what you've suggested later today
and let you know how it goes.

Many thanks to everyone who's responded to this thread.

Jon

Jul 23 '05 #6
JS
Hi again Rob

Used your code and it worked perfectly so many, many thanks. It really
is much appreciated.

All the best

Jon

Jul 23 '05 #7
JRS: In article <11**********************@o13g2000cwo.googlegroups .com>
, dated Wed, 23 Mar 2005 08:17:05, seen in news:comp.lang.javascript,
JS <go****@stanton.uk.net> posted :

Secondly, it appears that the + in (Math.round((ST.value * 14) +
PNDS.value) * 0.4536) is appending the PNDS.value to the ST.value * 14
so if PNDS.value is 0 and ST.value is 12 it calculates using 120.

FAQ, 4.21, refers; read the newsgroup FAQ before posting.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #8
JS
My apologies for posting this but I spent many hours searching for the
solution but was unable to. If it's in the FAQ then I was unable to
locate it. But, having posted here, RobG gave me exactly what was
needed so I feel that this group has served the exact purpose it
should. I'm not one for posting without first trying to solve the
problem.

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: martingerber | last post by:
Hi, I have the following script (javascript in html document): <html> <head> <meta http-equiv="content-type" content="text/html;charset=ISO-8859-1"> <title>Standort Speichern</title>
0
by: EasyRider41 | last post by:
I am trying to merge to scripting samples I for on a source code web site and having limited luck. Then first one is called Zebra Tables witch colors alternate rows of a table to look beter. The...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
5
by: Novice Computer User | last post by:
Hi. Can somebody PLEASE help. Here is a .php script. Right now, the minimum amount of time (i.e. duration) allowed is 1 month. However, I want to be able to reduce the minimum amount of time to...
2
by: reproots | last post by:
hi there, i am having trouble with a span command and would like to know if someone can help me, or point me in the correct direction. basically, i want a little textbox to pop up when a mouse...
3
by: cuties | last post by:
Hi all.... i'm very new to this programming language. i'm required to fulfill this task in the company i'm doing my practical. i hope i can get guide for my problem... Here is the script i...
14
by: issentia | last post by:
I'm working on this site: http://www.essenceofsoy.com/redesign/index2.html and I'm having a few problems with getting the layout exactly right. 1) When the menu items are rolled over, they...
2
by: kheitmann | last post by:
OK, so I have a blog. I downloaded the "theme" from somewhere and have edited a few areas to suit my needs. There are different font themes within the page theme. Long story short, my "Text Posts"...
0
by: richard12345 | last post by:
Hi Guys I have problem with site I am building. The sidebar with menu and other thinks is overlapping footer. The footer move with the content and but it dos it dos not move with the sidebar. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.