Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading