0

The problem is I don't know about the number of digits in floating point part in the output.. I can't assign 4 for example.. there might be 100 digits in floating point for instance n. How can I solve it?

 bcdiv('50','3',4); //16.6666
 bcdiv('50','3'); // correct output => 16.66666666666667

Thank you so much...

Sherif
  • 11,786
  • 3
  • 32
  • 57
user3754884
  • 87
  • 11
  • maybe interesting? [How to round/ceil/floor a bcmath number in PHP?](http://stackoverflow.com/questions/231057/how-to-round-ceil-floor-a-bcmath-number-in-php) – Ryan Vincent Oct 25 '15 at 13:34

2 Answers2

0

In floating point math you don't have exact precision. It's limited precision, because computers use a binary system instead of the decimal system we humans are used to. So while you and I can easily see that 10 / 1 is exactly 0.1, a computer sees it as 0.1000000000000000055511151231257827021181583404541015625. This is why we just pick some limited precision that we feel will be sufficient and ignore all of that other stuff at the end there.

See this answer to: Is floating point math broken? for more details.

Community
  • 1
  • 1
Sherif
  • 11,786
  • 3
  • 32
  • 57
0

The code is:

# Suppose the value is 125.987654321012354.
$num = 1250.987654321012354;
echo $num;
$num_array = explode(".", $num);

# Floating point count.
$fCountDigit = strlen(@$num_array[1]); # Use the @ sign if there is no floating point. When the value is integer 123351
$intNumber = $num_array[0];

Problem is: float has a specific limit of bytes of 4 bytes. So do keep that in mind.

For example:

# Suppose the value is 1250000000000.987654321012354.
$num = 1250000000000.987654321012354;
echo $num;

Output: 1250000000001 # no floating point.
TipuZaynSultan
  • 783
  • 4
  • 16