Mac Web Developer (Australia): PHP Example -- getting and setting floating point display precision using ini_get() and ini_set().
Source code
A PHP source code example of how to use ini_set() and ini_get() to control the display precision of floating point numbers.
The default formatting of a floating point number produces the following results when using echo():
Numbers less than 1
PHP default precision is set in the php.ini file and controlled via ini_get and ini_set.
2/3 = 0.666666666667 // PHP default precision found like so: ini_get("precision"); -->12
2/3 = 0.66667 // PHP floating point precision set like so: $old_precision = ini_set("precision", 5);
2/3 = 0.666666666667 // PHP floating point precision set like so: ini_set("precision", $old_precision);
These experiments indicate that ini_set CAN be used to control the number of displayed digits for numbers LESS than 1.
The following tests show how the formatting changes as the order-of-magnitude of the number changes.
1/10 = 0.1
1/100 = 0.01
1/1000 = 0.001
1/10000 = 0.0001
//I have yet to discover what controls the change from floating point format
//to scientific notation here.
1/100000 = 1E-05
1/1000000 = 1E-06
1/10000000 = 1E-07
These experiments indicate that there is a change to scientific notation at the threshold of 1E-5.
Numbers greater than 1
PHP default precision is set in the php.ini file and controlled via ini_get and ini_set.
1234567890 = 1234567890 // PHP default precision found like so: ini_get("precision"); -->12
1234567890 = 1234567890 // PHP floating point precision set like so: $old_precision = ini_set("precision", 5);
1234567890 = 1234567890 // PHP floating point precision set like so: ini_set("precision", $old_precision);
These experiments indicate that ini_set CANNOT be used to control the number of displayed digits for numbers GREATER than 1.
The following tests show how the formatting changes as the order-of-magnitude of the number changes.
I have yet to discover what controls the change from floating point format
to scientific notation here.
// PHP precision found like so: ini_get("precision"); -->12
12345678 = 12345678
123456789 = 123456789
1234567890 = 1234567890
12345678901 = 12345678901
123456789012 = 123456789012
1234567890123 = 1.23456789012E+12
12345678901234 = 1.23456789012E+13
Set precision to 10 with ini_set
// PHP precision found like so: ini_get("precision"); -->10
12345678 = 12345678
123456789 = 123456789
1234567890 = 1234567890
12345678901 = 1.23456789E+10
123456789012 = 1.23456789E+11
1234567890123 = 1.23456789E+12
12345678901234 = 1.23456789E+13
Set precision to 5 with ini_set
// PHP precision found like so: ini_get("precision"); -->5
12345678 = 12345678
123456789 = 123456789
1234567890 = 1234567890
12345678901 = 1.2346E+10
123456789012 = 1.2346E+11
1234567890123 = 1.2346E+12
12345678901234 = 1.2346E+13
Set precision to 3 with ini_set
// PHP precision found like so: ini_get("precision"); -->3
12345678 = 12345678
123456789 = 123456789
1234567890 = 1234567890
12345678901 = 1.23E+10
123456789012 = 1.23E+11
1234567890123 = 1.23E+12
12345678901234 = 1.23E+13
Following a suggestion by Alpha Angi I cast the numbers to float and try again.
This changes things when the precision is 5 and 3, but not when 10 and 12.
// PHP precision found like so: ini_get("precision"); -->12
12345678 = 12345678
123456789 = 123456789
1234567890 = 1234567890
12345678901 = 12345678901
123456789012 = 123456789012
1234567890123 = 1.23456789012E+12
12345678901234 = 1.23456789012E+13
Set precision to 10 with ini_set
// PHP precision found like so: ini_get("precision"); -->10
12345678 = 12345678
123456789 = 123456789
1234567890 = 1234567890
12345678901 = 1.23456789E+10
123456789012 = 1.23456789E+11
1234567890123 = 1.23456789E+12
12345678901234 = 1.23456789E+13
Set precision to 5 with ini_set
// PHP precision found like so: ini_get("precision"); -->5
12345678 = 1.2346E+07
123456789 = 1.2346E+08
1234567890 = 1.2346E+09
12345678901 = 1.2346E+10
123456789012 = 1.2346E+11
1234567890123 = 1.2346E+12
12345678901234 = 1.2346E+13
Set precision to 3 with ini_set
// PHP precision found like so: ini_get("precision"); -->3
12345678 = 1.23E+07
123456789 = 1.23E+08
1234567890 = 1.23E+09
12345678901 = 1.23E+10
123456789012 = 1.23E+11
1234567890123 = 1.23E+12
12345678901234 = 1.23E+13
Comments or corrections welcome. Send an email to farad'aty'farad.com.au.
|