Full Flavour Behaviour!

Millimetres to Inches PHP function
August 11th @ 9:44am

Just wrote this in the hope of stemming the tidal wave of Spineless Classics customer requests for measurements in inches from across the pond. I hope this is useful to somebody!

function mm2in($mm) {
	$divided = $mm/25.4;
	$sixteenths = round($divided/0.0625);
	$inches = floor($sixteenths / 16);
	$frac = '';
	
	if ($numerator = $sixteenths % 16) {
		$denominator = 16;
		
		while ($numerator%2 + $denominator%2 == 0) {
			// lazy way of reducing the fraction
			$numerator /= 2;
			$denominator /= 2;
		}
		if ($denominator < 5) {
			// some fractions have HTML codes
			$frac = '&frac'.$numerator.$denominator.';';
		} else {
			// otherwise fake with superscript/subscript
			$frac = '<sup>'.$numerator.'</sup><sub>'.$denominator.'</sub>';
		}
	}
	return $inches.$frac.' inch'.($inches==1&&$frac==''?'':'es');  // Yeah, I'm a pedant.
}

Try it out!mm

Comment on this entry

Don't miss..

Other Carl sites

Photo galleries