2012-08-23 44 views
0

我使用下面的代碼來轉換米到腳。它的作用像一個魅力,但我想在小數點後面添加英寸部分。如何在代碼之前和之後將小數點和英寸添加到小數點?

當前輸出

6.2 feet 

希望的輸出

6 feet 2 inches 

6'2" 

下面是代碼:

<?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) .")"; 
?> 
+1

[你有什麼試過?](http://whathaveyoutried.com) – 2012-08-23 17:17:39

回答

6

小數點後面的數字是而不是英寸,因爲一英尺有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); 
相關問題