0

こんにちは、以下を小数点以下 2 桁に丸めるにはどうすればよいですか。

echo
     "<div class='quote-results-result'>ex VAT  £" .
         ((($_POST['qtylitres'] *  $price ['Price']) + $fuelmargin['margin']) / 1.2) .
       "</div>"

PHP初心者の価格ビットを丸める必要があります。

4

4 に答える 4

3

次のように number_format 関数を使用します。

$number = 1234.56789
$new_number = number_format($number, 2, '.', '');
echo $new_number;

Output: 1234.57
于 2012-07-13T11:43:46.957 に答える
1

PHPマニュアルのラウンド関数を使用する

echo round(3.4);         // 3
echo round(3.5);         // 4
echo round(3.6);         // 4
echo round(3.6, 0);      // 4
echo round(1.95583, 2);  // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2);    // 5.05
echo round(5.055, 2);    // 5.06
于 2012-07-13T11:44:43.260 に答える
1

round($VARIABLE_NAME,2);//これは変数を小数点以下2桁に丸めます

于 2012-07-13T11:46:09.153 に答える
1
function roundoff($number,$format)
{  
    if($number != '') {
        $newNumber=$this->exp_to_dec($number);
        $num = explode('.',$newNumber);
        //pr($num);
        if(isset($num[1]) && $num[1] != 0) {    
            $dec = substr($num[1],0,$format);
        } else {
            $dec = '00';
        }
    } else {
        $num[0] = 0;
        $dec = '00';
    }
  return $num[0].'.'.$dec;
}

    function exp_to_dec($float_str) 

// 浮動小数点数文字列を 10 進表記でフォーマットし、符号付き浮動小数点数をサポートし、非標準のフォーマットもサポートします (例: 0.2e+2 for 20) { // 標準の php 浮動小数点文字列であることを確認します (つまり、0.2e+2 を 20 に変更します) // 浮動小数点数が特定の範囲内にある場合、php は自動的に浮動小数点数を 10 進数でフォーマットします $float_str = ( string ) (( float ) ($float_str));

    // if there is an E in the float string
    if (($pos = strpos ( strtolower ( $float_str ), 'e' )) !== false) {
        // get either side of the E, e.g. 1.6E+6 => exp E+6, num 1.6
        $exp = substr ( $float_str, $pos + 1 );
        $num = substr ( $float_str, 0, $pos );

        // strip off num sign, if there is one, and leave it off if its + (not required)
        if ((($num_sign = $num [0]) === '+') || ($num_sign === '-'))
            $num = substr ( $num, 1 );
        else
            $num_sign = '';
        if ($num_sign === '+')
            $num_sign = '';

    // strip off exponential sign ('+' or '-' as in 'E+6') if there is one, otherwise throw error, e.g. E+6 => '+'
        if ((($exp_sign = $exp [0]) === '+') || ($exp_sign === '-'))
            $exp = substr ( $exp, 1 );
        else
            trigger_error ( "Could not convert exponential notation to decimal notation: invalid float string '$float_str'", E_USER_ERROR );

    // get the number of decimal places to the right of the decimal point (or 0 if there is no dec point), e.g., 1.6 => 1
        $right_dec_places = (($dec_pos = strpos ( $num, '.' )) === false) ? 0 : strlen ( substr ( $num, $dec_pos + 1 ) );
        // get the number of decimal places to the left of the decimal point (or the length of the entire num if there is no dec point), e.g. 1.6 => 1
        $left_dec_places = ($dec_pos === false) ? strlen ( $num ) : strlen ( substr ( $num, 0, $dec_pos ) );

        // work out number of zeros from exp, exp sign and dec places, e.g. exp 6, exp sign +, dec places 1 => num zeros 5
        if ($exp_sign === '+')
            $num_zeros = $exp - $right_dec_places;
        else
            $num_zeros = $exp - $left_dec_places;

    // build a string with $num_zeros zeros, e.g. '0' 5 times => '00000'
        $zeros = str_pad ( '', $num_zeros, '0' );

        // strip decimal from num, e.g. 1.6 => 16
        if ($dec_pos !== false)
            $num = str_replace ( '.', '', $num );

    // if positive exponent, return like 1600000
        if ($exp_sign === '+')
            return $num_sign . $num . $zeros;

    // if negative exponent, return like 0.0000016
        else
            return $num_sign . '0.' . $zeros . $num;
    } // otherwise, assume already in decimal notation and return
    else
        return $float_str;
}
于 2012-07-13T12:07:52.487 に答える