How to convert integer number so that they have n decimal places in PostgreSQL SELECT - postgresql

How can numbers converted so that they have n decimal places?
I want to convert all numbers of a column so that they have three decimal places.
For example the query result should look like this
Amount
3.000
2.511
3.200
...

Please see this page: https://www.postgresql.org/docs/current/functions-formatting.html
select to_char(amount, '999D999') as "Amount"
from your_table;

Related

how to the get the Pentaho table values with Decimal

I am using Pentaho-code I am round the queries values as decimal but getting some colums values not decimal
table output:
NORMAL (06:00--17:00) 3 14341.54 43024.62
OFF_PEAK (22:00--06:00) 3 7002.39 21007.170000000002
PEAK (17:00--22:00) 3 9362.95 28088.850000000002
required output
NORMAL (06:00--17:00) 3 14341.54 43024.62
OFF_PEAK (22:00--06:00) 3 7002.39 21007.17
PEAK (17:00--22:00) 3 9362.95 28088.85
Column formats %.2f will definitely work to show 2 decimal point. You just need to be define column formats for every column. For string you need to define %s, for only real digit use %d, and to get 2 decimal you need to define %.2f etc.
You can have a look in my sample image for correct configuration-

Why are 2 numeric values with many decimal places considered equal in postgres?

Why is select (1.9999999999999999+2)/2 = (1.9999999999999999999999999999999999+2)/2 equal to true in postgres? Doing select pg_typeof((1.9999999999999999999999999999999999+2)/2) shows type numeric, which the docs say are exact numbers are should support thousands of digits after the decimal.
select 1.9999999999999999 = 1.9999999999999999999999999999999999 returns false as expected.
Why does select (1.999999999999999+2)/2 show 1.9999999999999995 like I'd expect but adding an extra "9" to the end shows 2.0000000000000000? Shouldn't the extra digit increase the precision and I should see the extra "9" in the result too?
In what cases can I be sure I won't see this in my queries when doing arithmetic on numeric types?
The numeric expression on the left side has only 16 decimal digits. The overall expression evaluation is limited to that precision.
Try this:
select (1.9999999999999999000000000000000000+2)/2 = (1.9999999999999999999999999999999999+2)/2
And you'll get false because you are now comparing 34 decimals on both sides (if I counted right).

Postgres Custom float type that is always truncated to 2 decimals after point

Can I generate a custom data type in postgres that everytime I insert or update a float into it it is truncate to 2 decimals after dot.
create table money(
formatted moneys_type
);
insert into money values (30.122323213);
Select * from money;
Returns
30.12
Update I didn't use numeric or decimal because they round up when 1.999 => 2
See documentation on Numeric Types / Arbitrary Precision Numbers.
The precision of a numeric is the total count of significant digits in
the whole number, that is, the number of digits to both sides of the
decimal point. The scale of a numeric is the count of decimal digits
in the fractional part, to the right of the decimal point. So the
number 23.5141 has a precision of 6 and a scale of 4. Integers can be
considered to have a scale of zero.
...
To declare a column of type numeric use the syntax:
NUMERIC(precision, scale)
The maximum allowed precision when explicitly specified in the type declaration is 1000.
So you can use
NUMERIC(1000, 2)

Getting float values out of PostgreSQL

I am having trouble retrieving float/real values out of PostgreSQL.
For example, I would like to store: 123456789123456, the retrieve that exact same number with a select statement.
table tbl (num real)
insert into tbl(num) values('123456789123456');
As it is now, if I "select num from tbl" the result is "1.23457e+14"
If I run "select CAST(num AS numeric) as num from tbl" the result is 123457000000000
If I run "select CAST(num AS float) as num from tbl" the result is 123456788103168 (where did this number come from)
How on earth can I select the value and get "123456789123456" as the result?
Thanks so much in advance
You declared the table with the column having a type of "real", which is a fairly low-precision floating-point number.
You probably want to use the type "double precision" (aka "float" or "float8") for a reasonable degree of floating-point accuracy. If you know the magnitude and precision of numbers you need to store, you may be better off declaring the column type as numeric(PREC,SCALE) instead - PREC being the total number of digits to keep, and SCALE the number of digits that will be to the right of the decimal point.
The real type has only 6 decimal digits of precision, so it can't store your number exactly. You may need to use "double precision" or "numeric/decimal" type.
Source: http://www.postgresql.org/docs/8.4/static/datatype-numeric.html .

Crystal Reports Formula: Getting the decimal and non-decimal part

For example I have a value of 103.33 I want to put 100 to one variable and 33 to another variable. How can I do this?
Thanks.
Create two formula fields, eg wholepart and decimalpart.
The formula for wholepart is trunc({yourfieldnamehere}) and the formula for decimalpart is {yourfieldnamehere} - trunc({yourfieldnamehere})
The value you get in decimalpart is going to be the decimal fraction; if you know it's always going to be a 2 digit decimal, multiply by 100. If it's variable, you could do a quick string conversion, count the digits and multiply by the appropriate power of 10.
Hampk
Use trunc to get Integer portion and then subtract to get decimal portion. Convert decimal portion to text by ToText and then take Split function to get after "." from decimal portion
or
Use ToText and use Split function to get after and before "."
You can also use the 'Remainder' function to find the number after the decimal point. For example, REMAINDER ({FIELD NAME},1) should give you 0.33