3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Postgresのdouble precision型を四捨五入したい

Last updated at Posted at 2020-02-28

はじめに

Postgresのdouble presision型のデータに対し、round関数で四捨五入するとうまくいかないため、対応した時のメモ

環境

  • Postgres 11

そのままやると

select round(col1,5) from test where id=1

するとこんなエラーが。

ERROR:  function round(double precision, integer) does not exist
LINE 7: select round(col1,5) from test where ...
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 290

原因

どうやら、Postgresにはdouble precisionを四捨五入する関数がないらしい。理由は参考URLに書いてある模様。

対応

無理やりnumericに変換してround関数に渡す♪ 

select round(CAST(col1 as numeric),5) from test where id=1

参考URL

How to round an average to 2 decimal places in PostgreSQL?

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?