PHP - Round more than 14 decimal places

871

Is it possible to round more than 14 decimal places?

If I do:

echo round(1/3, 4); // equals .3333
echo round(1/3, 14); // equals .33333333333333
echo round(1/3, 20); // equals .33333333333333

Is it possible to get 20 decimal places in PHP for absolute precision?

9

Answer

Solution:

For lots of decimal places, you'd better use BC Math rather than trying to force PHP to do it.

$result = bcdiv(1, 3, 20);

You can change the precision ini setting, but you won't get the results you're expecting.

345

Answer

Solution:

You can also use the decimal extension with a reasonably high precision like 28, which then supports up to 28 significant digits.

$decimal = new Decimal(1, 28) / 3;

echo $decimal->round(28); 

// 0.3333333333333333333333333333 

People are also looking for solutions to the problem: zip file created but not moving to folder in php

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.