Home > Net >  Format a division as a percent with decimal places
Format a division as a percent with decimal places

Time:06-15

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?

CodePudding user response:

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

  • Related