问题
When I execute a fetch from an Oracle database using PHP OCI, numbers that are less than 1 are shown as .XXXXXX,
e.g. .249999
. Is there a way to set this to 0.XXXXXX
or to any other format, without modifying every query to use to_char()
explicitly? (Perhaps through some session parameters?)
回答1:
There is no way to do what you're asking globally short of modifying php/oci-extension source code. The reason for this behavior is because oracle oci omits 0s in results and php converts all results from oci to strings without performing any casting depending on column datatype. Even results in SQL*Plus omit 0 by default, and SQL*Plus formatting has to be invoked with set numformat
to customize column formatting and prepend 0s.
There is currently no alter session parameter you can set to change this behavior.
Most common way to work around this is to use a wrapper around your queries and check for numeric columns with is_numeric
and then format the numeric column values with number_format
or sprintf
. Hopefully your application already uses a wrapper around stock php oci functions so you can make the change in one location.
回答2:
Using PHP you can easily add the 0, by converting to float:
$a = '.249999';
echo (float) $a;
Meaning you can convert your number by
$row['number'] = (float) $row['number'];
after fetching it from the DB.
来源:https://stackoverflow.com/questions/4284571/php-oci-oracle-and-default-number-format