Skip site navigation (1) Skip section navigation (2)

Re: float to numeric(7,3)

From: Bryan Lee Nuse <nuse(at)uga(dot)edu>
To: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: float to numeric(7,3)
Date: 2012-03-10 16:04:10
Message-ID: (view raw or whole thread)
Lists: pgsql-novice
Hello Steve,

Using the ST_Distance function from PostGIS ( 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);

     (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);

     (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,

In response to

pgsql-novice by date

Next:From: Tom LaneDate: 2012-03-10 16:14:53
Subject: Re: float to numeric(7,3)
Previous:From: Frank BaxDate: 2012-03-10 13:59:55
Subject: Re: float to numeric(7,3)

Privacy Policy | About PostgreSQL
Copyright © 1996-2015 The PostgreSQL Global Development Group