php как вывести строку в выходные и форматировать числа - PullRequest
0 голосов
/ 14 октября 2018

Привет, я пытаюсь отображать и выводить, где высота правильно отображается как 5 футов 8 дюймов, однако я не знаю, как это сделать.Я новичок в программировании, поэтому любая помощь будет оценена.

Конечный результат должен выглядеть следующим образом: Высота в футах и ​​дюймах: 5 футов 8 дюймов

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

1 Ответ

0 голосов
/ 14 октября 2018

Попробуйте следующее (объяснение в комментариях к коду):

// 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;
...