0

I have some data from database like this :

43966.31875

how to change 43966 to 0 and keep decimal number to 31875, What should I do?

well, for the final result like this :

0.31875

If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You

ProLuck
  • 331
  • 4
  • 18

2 Answers2

2

The regex method:

$string = '43966.31875';
echo preg_replace('/(\d+).(\d+)/i', '0.${2}', $string);

Will split number into 2 parts then replace the part 1 with "0."

The strstr method:

$string = '43966.31875';
echo '0'.strstr($string, '.');

Also split the number into 2 parts and take the second part and add "0" before

Huy Trịnh
  • 733
  • 3
  • 11
1

$number = 43966.31875;
$decimalPart = fmod($number, 1);

$decimalCount = explode('.', $number)[1];
$result = round($decimalPart, strlen($decimalCount));

Refer the Official Documentation: PHP fmod

Harish ST
  • 1,475
  • 9
  • 23