Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best method to always get 2 decimal places in Oracle?

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.

like image 779
Jack Avatar asked Dec 31 '25 20:12

Jack


1 Answers

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");
like image 136
Daan Avatar answered Jan 02 '26 10:01

Daan