Skip to content
Advertisement

Fraction formatting as halves or one third or quarters in PostgreSQL

I have some double-precision values in a column in PostgreSQL as following:

**amount**
*11.33
11.75
12.2
13.9
16.3
.....*

I want to round these values to their nearest quarter value at the decimal place. That is:

11.333569 -> 11.50
11.7555698 -> 11.75
12.236558 -> 12.25
13.925669 -> 13.75
16.101235 -> 16.25

I saw this kind of feature in MS Excel. It can be found at: ]

How can I do the same in PostgreSQL? Thanks in advance for your help.

Advertisement

Answer

You can use arithmetic:

select cast(round(x * 4) / 4.0 as decimal(10, 2))

Here is a db<>fiddle.

1 People found this is helpful
Advertisement