简体   繁体   English

删除除两个尾随零和千位分隔符之外的所有内容

[英]Remove all but two trailing zeros AND thousand separator

Postgres Version 11.7 Postgres 版本 11.7

I am trying to remove trailing zeros beyond two decimal places , while also adding thousand separator to my result set.我试图删除超过两位小数的尾随零,同时还在我的结果集中添加千位分隔符。

Example 1 produces the desired result;示例 1产生了预期的结果; all trailing zeros were removed:所有尾随零都被删除:

SELECT to_char(54354.0010, 'FM99 999 999 990.999999'); --> returns 54 354.001

Example 2 illustrates the problem:示例 2说明了问题:

SELECT to_char(54354.0000, 'FM99 999 999 990.999999'); --> returns 54 354.

In Example 2 all zeroes after the decimal are removed.示例 2中,小数点后的所有零都被删除。
But the desired result would be:但期望的结果是:

54 354.00

The result should always have a minimum of two decimal places, regardless whether they are zero or not.结果应始终至少保留两位小数,无论它们是否为零。

This produces your desired result:这会产生您想要的结果:

SELECT to_char(54354.0000, 'FM99 999 999 990.009999')

Two 0 instead of 9 after the decimal point.小数点后两个0而不是9
The manual: 手册:

9 digit position (can be dropped if insignificant) 9位 position(如果不重要可以去掉)
0 digit position (will not be dropped, even if insignificant) 0 digit position(不会被丢弃,即使微不足道)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM