如何在代码之前和之后将脚和英寸添加到小数?

ede*_*ede 0 php

我正在使用下面的代码将米转换为英尺.它就像一个魅力,但我想在小数点后添加英寸部分.

目前的输出

6.2 feet
Run Code Online (Sandbox Code Playgroud)

期望的输出

6 feet 2 inches 
Run Code Online (Sandbox Code Playgroud)

要么

6'2"
Run Code Online (Sandbox Code Playgroud)

这是代码:

<?php
$meters=$dis[height];
$inches_per_meter = 39.3700787;
$inches_total = round($meters * $inches_per_meter); /* round to integer */
$feet = $inches_total / 12 ; /* assumes division truncates result; if not use floor() */
$inches = $inches_total % 12; /* modulus */
echo "(". round($feet,1) .")"; 
?>
Run Code Online (Sandbox Code Playgroud)

Mar*_*ean 8

小数点后面的数字不是英寸,因为一英尺有12英寸.你想要做的是将厘米转换为英寸,然后将英寸转换为英尺和英寸.我按如下方式做到:

<?php

// this is the value you want to convert
$centimetres = $_POST['height']; // 180

// convert centimetres to inches
$inches = round($centimetres/2.54);

// now find the number of feet...
$feet = floor($inches/12);

// ..and then inches
$inches = ($inches%12);

// you now have feet and inches, and can display it however you wish
printf('You are %d feet %d inches tall', $feet, $inches);
Run Code Online (Sandbox Code Playgroud)