Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display quotient as a percentage

I am using Postgresql to generate a simple quotient of two fields " a / b " = -3 / 300 = -.01 or -1.00%. Instead it displays as zero. I've tried many variations of to_char with no success. How could I get "select -3/300 as quotient" to display the quotient in xxx.xxx% format? Thanks

like image 723
J-Ko Avatar asked Jul 10 '17 16:07

J-Ko


People also ask

How do I show percentage in PostgreSQL?

We have several options if we want to display numbers with a percentage sign in PostgreSQL. We can use the TO_CHAR() function to format the number along with the percentage sign. Or we can simply concatenate the number with the percentage sign, either with the CONCAT() function or with the concatenation operator.

How do I round in PostgreSQL?

How to Use ROUND() Function in PostgreSQL? To avail the functionalities of the ROUND() function, you have to follow the below syntax: ROUND(Number [ , n]); Here, in this syntax, the “Number” represents a numeric value to be rounded.


1 Answers

To display with the percentage sign, you can use the following format. Don't forget to first multiply by 100.0 so 1) is it indeed a percentage and 2) as noted by @dhke the computation is done with floats, not integers.

select to_char(100.0*-3/300,'999D99%') a;
    a
----------
   -1.00%
(1 row)
like image 123
JGH Avatar answered Nov 11 '22 10:11

JGH