【问题标题】:php how to echo string in output and format numbersphp如何在输出中回显字符串并格式化数字
【发布时间】:2018-10-14 18:04:47
【问题描述】:

您好,我正在尝试回显并输出高度正确显示为 5 英尺 8 英寸的位置,但我不知道如何执行此操作。 我是编程新手,因此将不胜感激。

最终结果应如下所示: 高度(英尺和英寸):5 英尺 8 英寸

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

【问题讨论】:

    标签: php string output echo


    【解决方案1】:

    尝试以下(代码 cmets 中的解释):

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

    【讨论】:

    • 是的,谢谢。唯一需要改变的是在 ft 和 $balanceInches 之间添加 ' '。
    • @NoviceProg14 乐于提供帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多