Shamsur Rahim Hemel
Shamsur Rahim Hemel

Reputation: 47

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.

Upvotes: 2

Views: 150

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

You can use arithmetic:

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

Here is a db<>fiddle.

Upvotes: 4

Related Questions