Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PostgreSQL中的双精度格式_Postgresql_Formatting_Floating Point - Fatal编程技术网

PostgreSQL中的双精度格式

PostgreSQL中的双精度格式,postgresql,formatting,floating-point,Postgresql,Formatting,Floating Point,我有一个包含3列的表: customer_name varchar ,account_type varchar ,current_balance double precision 当前_余额的示例值: 1200 1500.5 1500 它以我想要的方式格式化,但在开头添加了一个空格。如何解决这个问题?有没有更好的格式化方法 to_char(current_balance, 'FM9999999999999999D99') 发件人: FM:前缀填充模式(抑制填充 空格和零) 如果需要特

我有一个包含3列的表:

  customer_name varchar
 ,account_type varchar
 ,current_balance double precision
当前_余额的示例值:

1200 1500.5 1500 它以我想要的方式格式化,但在开头添加了一个空格。如何解决这个问题?有没有更好的格式化方法

to_char(current_balance, 'FM9999999999999999D99')
发件人:

FM:前缀填充模式(抑制填充 空格和零)

如果需要特定于语言环境的货币符号,请尝试
L

to_char(current_balance, 'FML9999999999999999D99')
L:货币符号(使用区域设置)

第8.4页针对名为
dbl
的列得出的结果,其值为12345.678,其中id=1:

>>> import psycopg2
>>> conn = psycopg2.connect(host='localhost', database='scratch', user='',password='')
>>> c = conn.cursor()

>>> c.execute("select to_char(dbl, '9999999999999999D99') from practice where id = 1;")
>>> c.fetchall() # with padding
[('            12345.68',)]

>>> c.execute("select to_char(dbl, 'FM9999999999999999D99') from practice where id = 1;")
>>> c.fetchall() # no padding
[('12345.68',)]

>>> c.execute("select to_char(dbl, 'FML9999999999999999D99') from practice where id = 1;")
>>> c.fetchall() # with locale-specific currency symbol
[('$12345.68',)]
可以使用删除多余的空格。在没有参数的情况下,它只删除空格

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)

正如已经在评论中指出的那样。这会给你带来麻烦。改用。

停在原地。小心双精度被视为浮点值。浮点数的存储和计算方式可能会导致精度下降。不要使用浮点值存储货币值。使用
NUMERIC
DECIMAL
类型。。如果我的输入记录为0(零),则该时间仅为.00。但是我想要0.00我怎么能这样做…?我不知道,事实上。我还在学习博士后的所有细节。如果所有其他方法都失败,您可以尝试一个
案例
,检查数字是否为零,如果为零,则返回“0.00”。迟到了,但有人可能会提到“99…0D99”作为0.00要求的可能解决方案。好的,谢谢您。。。我记下你的观点,然后在这里我将使用十进制。@ungalnanban:这与不使用十进制时的舍入错误有关,所以你最好这样做,因为如果会计总和错误,任何浮点到字符串的转换都将是你较小的问题。。。
>>> import psycopg2
>>> conn = psycopg2.connect(host='localhost', database='scratch', user='',password='')
>>> c = conn.cursor()

>>> c.execute("select to_char(dbl, '9999999999999999D99') from practice where id = 1;")
>>> c.fetchall() # with padding
[('            12345.68',)]

>>> c.execute("select to_char(dbl, 'FM9999999999999999D99') from practice where id = 1;")
>>> c.fetchall() # no padding
[('12345.68',)]

>>> c.execute("select to_char(dbl, 'FML9999999999999999D99') from practice where id = 1;")
>>> c.fetchall() # with locale-specific currency symbol
[('$12345.68',)]
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)