具有任意精度和一定比例的数字格式的 Oracle to_char 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9225711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:36:21  来源:igfitidea点击:

Oracle to_char function with a number format that has an arbitrary precision and certain scale

oracleroundingnumber-formatting

提问by Korhan Ozturk

This one is pretty simple actually yet I wasn't able to find anything useful.

这个实际上很简单,但我找不到任何有用的东西。

In my SQL query I have some rounded numbers with a single scale value - round(number,1). If the numbers are rounded to some decimal digit it prints in the format '9,9'.

在我的 SQL 查询中,我有一些带有单个比例值的四舍五入数字 - round(number,1)。如果数字四舍五入为某个十进制数字,则以“9,9”格式打印。

On the other hand if the numbers are rounded to an integer, only the integer value without the zero after comma is printed although I want my query to select the numbers in '9,9' format even the decimal digit is zero.

另一方面,如果数字四舍五入为整数,则只打印逗号后没有零的整数值,尽管我希望我的查询选择“9,9”格式的数字,即使十进制数字为零。

In short, I think I need something like for example

简而言之,我想我需要类似的东西

to_char((select round(121.01,1), from dual), '*,1') ;to output 121,0.

to_char((select round(121.01,1), from dual), '*,1') ;输出121,0

What is the best way to do this? Thanks in advance

做这个的最好方式是什么?提前致谢

Korhan

科尔汗

采纳答案by Ben

All you have to do is specify the number of decimal points you want in your to_char. The problem with using format masks is that you need to specify the number of numbers you want in front of your decimal point.

您所要做的就是在您的to_char. 使用格式掩码的问题是您需要在小数点前指定所需的数字数量。

SQL> select to_char(round(121.01,1),'999.9') from dual;

TO_CHA
------
 121.0

SQL> select to_char(round(121.4,1),'999.9') from dual;

TO_CHA
------
 121.4

SQL> select to_char(round(121,1),'999.9') from dual;

TO_CHA
------
 121.0

SQL> select to_char(round(5121,1),'999.9') from dual;

TO_CHA
------
######

SQL>

There are a number of other formatting options.

还有许多其他格式选项

回答by Szilard Barany

Use 0instead 9for decimal places:

使用0而不是9作为小数位:

SELECT TO_CHAR( ROUND( 121.01, 1 ), '990D0' ) num FROM DUAL;

NUM  
------
121.0

回答by Srinivasan.S

This simple query may help you,

这个简单的查询可能对你有帮助,

select to_char(round(121.01,1), '999.0') from dual;

In to_charfunction:

to_char函数中:

9 - indicate to block/hide zeros in the output.

9 - 指示阻止/隐藏输出中的零。

0 - indicate to show zero in the output at anywhere in before/after decimal point.

0 - 表示在输出中小数点前/后的任何位置显示零。

Note:

笔记:

No. of '9/0's in before/after decimal point is number of digits which you want to display beore/after decimal point.

小数点前/后的'9/0'数是您要显示的小数点前/后的位数。