0

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.

SoulAiker
  • 139
  • 1
  • 10
  • Does this answer your question? [How do I convert a string to a number in PHP?](https://stackoverflow.com/questions/8529656/how-do-i-convert-a-string-to-a-number-in-php) – solid.py Mar 10 '20 at 10:35

2 Answers2

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%

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
  • I guess @Morgan you didn't understand my question. what i mean for that. why the output is 5%, is it possible to make the 5% to 5.0%? – SoulAiker Mar 10 '20 at 09:04
1

you can use number_format

$first_wtd_item_final_result = (number_format(round($first_wtd_item_computation3,1), 1).'%');
Sok Hai
  • 546
  • 4
  • 12