455,629 Members | 1,703 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,629 IT Pros & Developers. It's quick & easy.

# Help needed for the weight conversion script

 P: n/a 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): cms or inches Here's my code for KGS to STONES/POUNDS (not working): KGM or St lbs 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 Replies

 P: n/a "JS" 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

 P: n/a 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

 P: n/a "JS" 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: titles.htm Jul 23 '05 #4

 P: n/a 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): cms or The correct abbreviation for centimetre is "cm" and the correct abbreviation for kilograms is "kg". inches Almost any use of 'eval' is wrong or not required, see below. Here's my code for KGS to STONES/POUNDS (not working): [...] 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:
All values rounded to nearest whole number
cm converts to inches
kg converts to St lbs

-- Rob Jul 23 '05 #5

 P: n/a 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

 P: n/a 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

 P: n/a JRS: In article <11**********************@o13g2000cwo.googlegroups .com> , dated Wed, 23 Mar 2005 08:17:05, seen in news:comp.lang.javascript, JS 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 * 14so 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 © JL/RC: FAQ of news:comp.lang.javascript jscr maths, dates, sources. TP/BP/Delphi/jscr/&c, FAQ items, links. Jul 23 '05 #8

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion.