在PostgreSQL中格式化双精度 [英] Format double precision in PostgreSQL

查看:387
本文介绍了在PostgreSQL中格式化双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  customer_name varchar 
,account_type varchar
,current_balance双精度

current_balance的示例值:

 
1200
1500.5
1500

我希望它们能像这样显示:

 
1200.00
1500.50
1500.00

我试过以下查询:

$ p $ SELECT to_char(current_balance,'9999999999999999D99')
FROM bank;

它按照我想要的方式进行格式化,但在开始时添加一个空格。如何解决这个问题?有没有更好的方式来格式化?

解决方案

您可以使用 trim 删除多余的空格。没有参数,它只删除空格。

  charles =#SELECT to_char(12345.67,'99999999999999999D99'); 
to_char
-----------------------
12345.67
(1 row)

charles =#SELECT trim(to_char(12345.67,'99999999999999999D99'));
btrim
----------
12345.67
(1 row)


I have a table with 3 columns:

  customer_name varchar
 ,account_type varchar
 ,current_balance double precision

Example values for current_balance:

1200
1500.5
1500

I want them to display like this:

1200.00
1500.50
1500.00

I tried the following query:

SELECT to_char(current_balance,'9999999999999999D99')
  FROM bank;

It formats the way I want but adds a space at the beginning. How to solve this? Is there a better way to format?

解决方案

You can use trim to remove the extra spaces. With no arguments, it removes only spaces.

charles=# SELECT to_char(12345.67,'99999999999999999D99');
        to_char
-----------------------
              12345.67
(1 row)

charles=# SELECT trim(to_char(12345.67,'99999999999999999D99'));
  btrim
----------
 12345.67
(1 row)

这篇关于在PostgreSQL中格式化双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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