Re: float to numeric(7,3)

Поиск
Список
Период
Сортировка
От Bryan Lee Nuse
Тема Re: float to numeric(7,3)
Дата
Msg-id 91A24D6F-E530-45E2-A39D-8E6EDDDB0760@uga.edu
обсуждение исходный текст
Ответ на float to numeric(7,3)  (Steve Horn <steve@stevehorn.cc>)
Список pgsql-novice
Hello Steve,

Using the ST_Distance function from PostGIS (http://www.postgis.org/docs/ST_Distance.html) which returns a float.

I would like to return the result of this function rounded to 3 decimal places. What is the best way to do that?


I can't claim it's the best way, but have you tried the following?  Substituting a different function for ST_Distance, for this example:


     sar=> SELECT round(pi()::numeric,3);

      round 
     -------
      3.142
     (1 row)


If you want to specify the number of decimal places using round(), you have to cast the value as numeric. 
You could cast the result directly to numeric(7,3) if you wanted:

     sar=> SELECT (pi()*1000)::numeric(7,3);

      numeric  
     ----------
      3141.593
     (1 row)


...but that will fail if your value is too large:

     sar=> SELECT (pi()*10000)::numeric(7,3);
     
     ERROR:  numeric field overflow
     DETAIL:  A field with precision 7, scale 3 must round to an absolute value less than 10^4.


Hope that helps,
Bryan

В списке pgsql-novice по дате отправления:

Предыдущее
От: Frank Bax
Дата:
Сообщение: Re: float to numeric(7,3)
Следующее
От: Tom Lane
Дата:
Сообщение: Re: float to numeric(7,3)