What is the best method to always get 2 decimal places in values returned by Oracle?
At the moment I am wrapping all database related PHP functions in number_format etc. but I want to move these to be directly inside the SQL query.
Even better is there an environment variable I can set or similar when I am connecting to Oracle so that I do not have to do this?
function OrderNetTotal($id) {
global $dbh;
$sth = $dbh->prepare("SELECT net_total FROM order_totals WHERE order_no = $id");
$sth->execute();
$result = $sth->fetchAll();
return number_format((float)$result[0]['0'], 2, '.', '');
}
Note: I cannot edit database schema etc.
Well you could just use ROUND()
$sth = $dbh->prepare("SELECT ROUND(net_total,2) FROM order_totals WHERE order_no = $id");
Or even better:
$sth = $dbh->prepare("SELECT TO_CHAR(net_total,'99.99') FROM order_totals WHERE order_no = $id");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With