Skip to content
Advertisement

PostgreSQL- Round REAL data type (yes, I know numeric exist)

I know REAL data type is not accurate and normally for currency I should use numeric data type. But, I’m asked to do some stuff and one of the conditions is that the data type is real. When I try to do round((....),2) for example, I get that round function does not exist for this data type. My question is, without converting, is there any function that can return a REAL value rounded to 0?

Many thanks!1

Advertisement

Answer

As you can see here it’s no way to round without any type cast. It’s only two kinds of function exists:

round(dp or numeric) – round to nearest integer

round(v numeric, s int) – round to s decimal places

Real = double precision. So you need to use convert anyway if you want to get some decimal places:

select round('123.456789'::real::numeric,2)

upd. Keep care about rounding+cast at big real numbers:

select round('12122156.567'::real::numeric, 2); --< rounding up to 6 digits, result = 12122200
select round('12122156.567'::real::DOUBLE PRECISION::numeric,2); --<< rounding result = 12122157

Or you can use round without decimal places:

select round('123.456789'::real)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement