在 Oracle 中始终获得 2 个小数位的最佳方法是什么? [英] What is the best method to always get 2 decimal places in Oracle?

查看:40
本文介绍了在 Oracle 中始终获得 2 个小数位的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Oracle 返回的值中始终获得 2 个小数位的最佳方法是什么?

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

目前我正在用 number_format 等包装所有与数据库相关的 PHP 函数,但我想将这些函数直接移到 SQL 查询中.

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.

更好的是,当我连接到 Oracle 时,是否有我可以设置或类似的环境变量,这样我就不必这样做了?

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.

推荐答案

好吧,你可以使用 ROUND()

$sth = $dbh->prepare("SELECT ROUND(net_total,2) FROM order_totals WHERE order_no = $id");

甚至更好:

$sth = $dbh->prepare("SELECT TO_CHAR(net_total,'99.99') FROM order_totals WHERE order_no = $id");

这篇关于在 Oracle 中始终获得 2 个小数位的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆