skip to Main Content

I got problem why is it happen when I convert the number of ROUND(4.9813200498132,1) in PhpMyAdmin it result 5.0 then when i try to convert the value in php it result 5 only. how to make the result 5.0 to the php side.? is it need to convert the string number to int number? to understand well i will share to you guys the sample equation that i create.

Equation in php:

$first_wtd_item_first_option =  str_replace( ',', '', $high_item_d_wtd_data->total_qty );
$first_wtd_item_second_option = str_replace( ',', '', $high_item_a_wtd_data->total_qty );
$first_wtd_item_third_option =  str_replace( ',', '', $high_item_b_wtd_data->total_qty );

$first_wtd_item_computation1 = $first_wtd_item_second_option + $first_wtd_item_third_option;
$first_wtd_item_computation2 = ($first_wtd_item_first_option / $first_wtd_item_computation1);
$first_wtd_item_computation3 = $first_wtd_item_computation2 * 100;
$first_wtd_item_final_result = (round($first_wtd_item_computation3,1).'%');

Output:

rounded

Thank you.

2

Answers


  1. You should multiple with 10, not with 100, also you can use number_format()

    $first_wtd_item_first_option =  str_replace( ',', '', $number);
    $first_wtd_item_second_option = str_replace( ',', '', $number);
    $first_wtd_item_third_option =  str_replace( ',', '', $number);
    
    $first_wtd_item_computation1 = $first_wtd_item_second_option + $first_wtd_item_third_option;
    $first_wtd_item_computation2 = ($first_wtd_item_first_option / $first_wtd_item_computation1);
    $first_wtd_item_computation3 = number_format((float) $first_wtd_item_computation2 * 10, 1, '.', '');
    $first_wtd_item_final_result = $first_wtd_item_computation3.'%';
    

    Result:

    5.0%

    Login or Signup to reply.
  2. you can use number_format

    $first_wtd_item_final_result = (number_format(round($first_wtd_item_computation3,1), 1).'%');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search