Doug Fir
Doug Fir

Reputation: 21212

Format a division as a percent with decimal places

I'm finding this surprisingly difficult.

select 10 / 100 as ten_pct

Returns just 0.

I want 0.1. Tried:

select round(10 / 100, 2) as ten_pct

Also returns 0.

select (10 / 100)::float as ten_pct

Also returns 0

select (10 / 100)::decimal as ten_pct

Also returns 0

How can I return 0.1?

Upvotes: 2

Views: 288

Answers (1)

user330315
user330315

Reputation:

10 / 100 is an integer division and returns an integer. You need to make at least one number a decimal before you divide them

10.0 / 100 or 10 / 100::decimal or 10 / 100.0

Upvotes: 3

Related Questions