簡體   English   中英

PHP如何在輸出和格式數字中回顯字符串

[英]php how to echo string in output and format numbers

嗨,我正在嘗試回顯和輸出高度正確顯示為5ft 8ins的位置,但是我不知道如何執行此操作。 我是編程新手,所以將不勝感激。

最終結果應類似於:以英尺和英寸為單位的高度:5英尺8英寸

$heightMeters = 1.75;
$heightInches = $heightMeters * 100 /2.54;
$heightFeet = $heightInches / 12;
echo 'Height in Feet and inches: '.$heightFeet;

請嘗試以下操作(代碼注釋中的解釋):

// given height in meters
$heightMeters = 1.75;

// convert the given height into inches
$heightInches = $heightMeters * 100 /2.54;

// feet = integer quotient of inches divided by 12
$heightFeet = floor($heightInches / 12);

// balance inches after $heightfeet
// so if 68 inches, balance would be remainder of 68 divided by 12 = 4
$balanceInches = floor($heightInches % 12);

// prepare display string for the height
$heightStr = 'Height in Feet and inches: ';
// If feet is greater than zero then add it to string
$heightStr .= ($heightFeet > 0 ? $heightFeet . 'ft ' : '');
// If balance inches is greater than zero then add it to string
$heightStr .= ($balanceInches > 0 ? $balanceInches . 'ins' : '');

// Display the string
echo $heightStr;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM