2015-06-04 103 views
1

我试图使用PHP将厘米转换为英尺和英寸。使用PHP将厘米转换为英尺和英寸使用PHP

这是我用于计算的函数。

function getMeasurements($cm) { 

    $inches = ceil($cm/2.54); 
    $feet = floor(($inches/12)); 
    $measurement = $feet."' ".($inches%12).'"'; 

    return $measurement; 
} 

我调用这个函数是这样的:

$cm = 185; 
echo "My Height = ".getMeasurements($cm); 

问题是在调用此之后,我能得到一个结果像这个 -

我的身高= 6' 73"

看英寸它的不正确。有人可以告诉我是什么原因得到这样的结果。

谢谢。

+0

'$英寸'是总英寸数。也许减去它的脚数x 12,首先,如果你想要它代表余数? –

回答

9
[[email protected] tmp]$ cat test.php 
<?php 

function cm2feet($cm) 
{ 
    $inches = $cm/2.54; 
    $feet = intval($inches/12); 
    $inches = $inches%12; 
    return sprintf('%d ft %d ins', $feet, $inches); 
} 

echo cm2feet(162) 

?> 

输出

[[email protected] tmp]$ php test.php 
5 ft 3 ins 
+0

输出是'5英尺63英寸' – TNK

+0

如何?做谷歌https://www.google.co.in/search?q=cm+to+feet&ie=utf-8&oe=utf-8&gws_rd=cr&ei=5-RvVdHDOc-xuATcjoAY –

+0

对不起我的错误...我用过http ://phpfiddle.org/进行编译。这是错误..我的编码也为我工作 – TNK

1

出现这种情况的原因是,当你在phpfiddle.org运行它,它是不是一个真正的PHP环境。为了解决这个问题,你应该在你的模数运算符周围添加空格。

+1

谢谢你提到它。 – TNK

相关问题