Templar
Templar

Reputation: 1871

Show more digits in PHP

Let's say I have:

echo 1/3;

And it print out only 0.33333333333333, can I get more digits?

Upvotes: 3

Views: 876

Answers (4)

Gordon
Gordon

Reputation: 316969

Can use bcdiv

echo bcdiv(1, 3, 20);

The third argument

is used to set the number of digits after the decimal place in the result. You can also set the global default scale for all functions by using bcscale().

Upvotes: 6

Stefan Gehrig
Stefan Gehrig

Reputation: 83622

Edit the precision configuration variable either in your php.ini or some other configuration location or use ini_set().

ini_set('precision', 22);
echo 1/3;
// 0.3333333333333333148296

Even though I highly doubt that you really need that kind of precision ;-)

EDIT

As Gordon said: you'll hit the floating point precision limit in PHP sooner or later (depending on the precision specified). So the better way would be to use either the BCMath Arbitrary Precision Mathematics extension or the GNU Multiple Precision extension, if you're after real high precision mathematics.

Upvotes: 3

Álvaro González
Álvaro González

Reputation: 146390

The setting is precision: http://es.php.net/manual/en/ini.core.php

However, I would not use it except for debugging purposes. Have a look at number_format()

Upvotes: 1

jcoder
jcoder

Reputation: 30035

You might want tto look into the BC arbitary precision php library http://php.net/manual/en/book.bc.php

Upvotes: 2

Related Questions