舍入一个数字并在oracle中保留尾随零

时间:2017-07-26 08:54:30

标签: sql database oracle11g rounding

好的,所以(比方说)我有一个像 7836.721 这样的数字。我想要一个oracle查询,它可以给我以下结果:

component.leftTargetAction = (self,#selector(ViewController.myAction))

该数字应向上舍入到一个位置,小数点后的所有数字都会转换为零。

我用过的东西:

7837.000

给了我:

select round(7836.721,0) from dual

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

听起来你需要to_char ......

SELECT to_char(TRUNC(7836.721), '9999990.000') numbr
FROM   dual;

NUMBR
------------
    7837.000

这确实假设你在任何数字的3位小数后,而不是7.12到7.00。

ETA:

这似乎是一个非常奇怪的要求,但是你走了:

WITH sample_data AS (SELECT 123 numbr FROM dual UNION ALL
                     SELECT 123.12 numbr FROM dual UNION ALL
                     SELECT 123.1234 numbr FROM dual UNION ALL
                     SELECT 123.12345 numbr FROM dual UNION ALL
                     SELECT 0.12345 numbr FROM dual UNION ALL
                     SELECT 1.2345678901234567890123456789012345678901234567890123e50 numbr FROM dual UNION ALL
                     SELECT NULL numbr FROM dual UNION ALL
                     SELECT 1.23e-50 numbr FROM dual)
SELECT numbr,
       ROUND(numbr) || CASE WHEN numbr != TRUNC(numbr) THEN 
                                 RPAD('.', LENGTH(numbr -TRUNC(numbr)), '0') END new_numbr
FROM   sample_data;

     NUMBR NEW_NUMBR
---------- --------------------------------------------------------------------------------
       123 123
    123.12 123.00
  123.1234 123.0000
 123.12345 123.00000
   0.12345 0.00000
1.23456789 1.2345678901234567890123456789012346E+50

  1.23E-50 0.000000000000000000000000000000000000000