length and precision issue in Postgres - postgresql

I'm using postgres sql I need 12 digits and after decimal I need only 6 digits what length & Precision should I give in columns.what datatype shold I give to cloumn.
I tried numeric as a datatype and length I give to column is 12 and precision is 6.

If you need 12 digits before the decimal and 6 digits after, you need numeric(18,6)
Quote from the manual
The scale of a numeric is the count of decimal digits in the fractional part, to the right of the decimal point. 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
(Emphasis mine)
So the first number (precision) in the type definition is the total number of digits. The second one is the number of decimal digits.
If you specify numeric(12,6) you have a total of 12 digits and 6 decimal digits, which leaves you only 6 digits for the digits to the left of the decimal. Therefor you need numeric(18,6)

Related

printf: how to set the default number of digits used by the exponent?

printf: is it possible to configure the default number of digits used by the exponent.
For portability reason I would like to set the number of digits used for exponents for exponents below 100.
On my machine the default is 2 digits
printf "%.3e\n", 342.7234;
# 3.427e+02
but in How can I convert between scientific and decimal notation in Perl? the exponent has 3 digits.

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)

"Round" 2530.30 to 2599 in Postgres

I need to replace numbers like 2530.30 with 2599 in PostgreSQL.
I tried using ROUND(2530.30)+0.99 but it only changes the numbers after the decimal point to 99. So it results in 2530.99, which I don't want.
I want to remove fractional digits and replace the last two decimal digits with 99. I know I can just use an integer, but my assignment at school says I need to do this.
There should no be negative numbers, the assignment says that I should have a product that is sold for, let's say, 3500.50 dollars, I then need to make this number go from 3500.50 to 3599. Not 3500.99.
Divide by 100, truncate, multiply by 100 again:
SELECT trunc(2530.30 / 100) * 100 + 99;
This replaces all numbers in the range [2500, 2600) with 2599.
Or, in more general terms, it replaces the last two decimal digits with 99 and discards fractional digits (which also transforms 0 or 12.50 to 99).
Negative numbers cannot occur, as you say, so ignored.

how to remove last zero from number in matlab

If I set a variable in Matlab, say var1 = 2.111, after running the script, Matlab returns var1 = 2.1110. I want Matlab to return the original number, with no trailing zero. Anyone know how to do this. Thanks in advance.
By default Matlab displays results in Short fixed decimal format, with 4 digits after the decimal point.
You can change that to various other format such as:
long
Long fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
3.141592653589793
shortE
Short scientific notation, with 4 digits after the decimal point.
Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.
3.1416e+00
longE
Long scientific notation, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.
3.141592653589793e+00
shortG
The more compact of short fixed decimal or scientific notation, with 5 digits.
3.1416
longG
The more compact of long fixed decimal or scientific notation, with 15 digits for double values, and 7 digits for single values.
3.14159265358979
shortEng
Short engineering notation, with 4 digits after the decimal point, and an exponent that is a multiple of 3.
3.1416e+000
longEng
Long engineering notation, with 15 significant digits, and an exponent that is a multiple of 3.
3.14159265358979e+000
However I don't think other options are available. If you absolutely want to remove those zeros you would have to cast you result in a string and remove the trailing 0 characters and then display your result as a string and not a number.

Double double precision in PostGis in Postgresql

This is my SQL:
SELECT st_asText(ST_GeomFromText('POINT(52.000000000012345678 21.0000000000123456789)'))
SELECT st_asText(ST_MakePoint(52.000000000012345678, 21.0000000000123456789))
But response is:
POINT(52.0000000000123 21.0000000000123)
I need double double precision in PostGis. How can i fix it?
That is already double precision. Single precision coordinates would trim after the sixth decimal whereas double offers 15 digits of precision. You're trying to set a point with 18 decimal positions.
Also is important to note that the number of decimal places a double can hold depends on the numbers to the left of the decimal separator. (see OSGeo rants abot that) so you're using two digits for the integer part (52 and 21) and you have 13 digits left to play with, which is exactly what you're getting in the response.