Mac/Macintosh educational/tutor software developer

Faraday R&D

| Home | Source Code |

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.66666666666667   // PHP default precision found like so: ini_get("precision"); -->14
2/3 = 0.66667   // PHP floating point precision set like so: $old_precision = ini_set("precision", 5);
2/3 = 0.66666666666667   // 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 = 1.0E-5
1/1000000 = 1.0E-6
1/10000000 = 1.0E-7
 
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 = 1234567890123
12345678901234 = 12345678901234

 
Set precision to 10 with ini_set
 
   // PHP precision found like so: ini_get("precision"); -->10
12345678 = 12345678
123456789 = 123456789
1234567890 = 1234567890
12345678901 = 12345678901
123456789012 = 123456789012
1234567890123 = 1234567890123
12345678901234 = 12345678901234

 
Set precision to 5 with ini_set
 
   // PHP precision found like so: ini_get("precision"); -->5
12345678 = 12345678
123456789 = 123456789
1234567890 = 1234567890
12345678901 = 12345678901
123456789012 = 123456789012
1234567890123 = 1234567890123
12345678901234 = 12345678901234

 
Set precision to 3 with ini_set
 
   // PHP precision found like so: ini_get("precision"); -->3
12345678 = 12345678
123456789 = 123456789
1234567890 = 1234567890
12345678901 = 12345678901
123456789012 = 123456789012
1234567890123 = 1234567890123
12345678901234 = 12345678901234

 
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+7
123456789 = 1.2346E+8
1234567890 = 1.2346E+9
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+7
123456789 = 1.23E+8
1234567890 = 1.23E+9
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.
 
 
| Copyright © 2004, Faraday R&D. | Feedback |