user7722505
user7722505

Reputation:

"Round" 2530.30 to 2599 in Postgres

I need to replace numbers like 2530.30 with 2599 in PostgreSQL.

I tried using ROUND(2530.30)+0.99 but it only changes the numbers after the decimal point to 99. So it results in 2530.99, which I don't want.

I want to remove fractional digits and replace the last two decimal digits with 99. I know I can just use an integer, but my assignment at school says I need to do this.

There should no be negative numbers, the assignment says that I should have a product that is sold for, let's say, 3500.50 dollars, I then need to make this number go from 3500.50 to 3599. Not 3500.99.

Upvotes: 0

Views: 63

Answers (1)

Erwin Brandstetter
Erwin Brandstetter

Reputation: 657202

Divide by 100, truncate, multiply by 100 again:

SELECT trunc(2530.30 / 100) * 100 + 99;

This replaces all numbers in the range [2500, 2600) with 2599.
Or, in more general terms, it replaces the last two decimal digits with 99 and discards fractional digits (which also transforms 0 or 12.50 to 99).

Negative numbers cannot occur, as you say, so ignored.

Upvotes: 1

Related Questions