Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conversion to imperial units (fractions)

Status
Not open for further replies.

ttuser4

MIS
Jun 19, 2008
147
CA
hi, does anybody have a javascript code for conversion from metric to imperial system using fractions (like 51 3/16 etc)?
I have one in vb6 one in php but I need one in javascript (i want to convert one desktop application into web).
 
If you already have the script in VB and PHP it should be simple to adapt them to javascript.

The formulas remain the same.

1 lb = 2.2 kg
1 inch = 2.54 inches.









----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Actually both values are incorrect.

It should be:

1kg = 2.2 pounds
and 1 in = 2.54 cm


I have no idea what was going on with me when I posted that.

Sorry about that.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
can you help me to "convert" this php into javascript?

<?php
$mm=35831; #input value, for example 35831

$denom = 16;
$rnd = 1.0/$denom;

$in = $mm / 25.4;
$in_f = $in - floor($in);
$in_i = floor($in);

$n = ceil($in_f / $rnd);

$dv = gcd($n, $denom);
$n /= $dv;
$denom /= $dv;

$in_str = "{$n}/{$denom}";

$inches = $mm."mm=".$in_i." ".$n."/".$denom." inch";
echo $inches;

function gcd($a, $b) {
if ($a == 0)
return $b;
while($b > 0 && $a > 0) {
if ($a > $b)
$a -= $b;
else
$b -= $a;
}
return $a;
}
?>
 
Something like this?

Code:
<html>
<head>

	<script type="text/javascript">

		var mm = 35831;		// input value, for example 35831

		var denom = 16;
		var rnd = 1.0 / denom;

		var in_ = mm / 25.4;
		var in_f = in_ - Math.floor(in_);
		var in_i = Math.floor(in_);

		var n = Math.ceil(in_f / rnd);

		var dv = gcd(n, denom);
		n /= dv;
		denom /= dv;

		var in_str = n + '/' + denom;

		var inches =  mm + 'mm=' + in_i + ' ' + n + '/' + denom + ' inch';
		document.write(inches);

		function gcd(a, b) {
			if (a == 0) return b;
			while(b > 0 && a > 0) {
				if (a > b) {
					a -= b;
				} else {
					b -= a;
				}
			}
			return a;
		}

	</script>

</head>
</html>

The only changes were:

- Defining variables with var and removing $ (although $ is a valid variable prefix in JS, I dislike it outside of PHP).

- Prefixing floor and ceil with "Math."

- String manipulation tweaking

- Renaming the variable 'in'.

Hope this helps,
Dan



-->

Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top