1

I am trying to truncate numbers in PostgreSQL with:

SELECT trunc(31.71429,15); 

I have this output:

31.714290000000000

But in Oracle I have this:

31.71429

How can have the same output as in Oracle?

Kamfasage
  • 181
  • 1
  • 7
  • 14
  • It is really the matter of the data representation, not the data values. And various clients could to represent it in various ways (with or without trailing zeros) – Abelisto Aug 17 '17 at 17:31
  • [So do you have your answer?](http://meta.stackexchange.com/a/5235/169168) – Erwin Brandstetter Aug 27 '17 at 14:07
  • yes the only way was to cast into double precision.. But i still don't know what information will be lost since right now it treats simple data – Kamfasage Sep 05 '17 at 08:17

4 Answers4

2

This is behave of Numeric data type. The most easy solution - with possible lost some small precision is cast to double precision:

postgres=# SELECT trunc(31.71429,15);
┌────────────────────┐
│       trunc        │
╞════════════════════╡
│ 31.714290000000000 │
└────────────────────┘
(1 row)

postgres=# SELECT trunc(31.71429,15)::double precision;
┌──────────┐
│  trunc   │
╞══════════╡
│ 31.71429 │
└──────────┘
(1 row)
Pavel Stehule
  • 42,331
  • 5
  • 91
  • 94
2

The value is correct, your "problem" is with the display - the text representation. So use to_char() to format numbers to your liking. You can use the display modifier FM to suppress insignificant '0':

SELECT to_char(trunc(31.71429,15), 'FM9999999999.999999999999999') 

Produces the desired result - as text for display (up to 10 digits before and 15 after the comma - add more if there can be more):

'31.71429'

BTW, the manual is a bit misleading about that. It says:

prefix fill mode (suppress leading zeroes and padding blanks)

But 'FM' also suppresses insignificant trailing zeroes. I filed a documentation bug report.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
  • it works but the problem is that i do a "SUM" of numeric values so it does not work – Kamfasage Aug 18 '17 at 08:56
  • @Kamfasage: Sum before `to_char()` and it works. `to_char(sum(trunc(...)), ...)`. Typically, you would also sum before truncating so not to lose more precision than necessary, so: `to_char(trunc(sum(...), ...))`. – Erwin Brandstetter Aug 18 '17 at 11:20
2

As stated in this other thread, you can now use the new trim_scale(numeric) function starting with PostgreSQL 13.

e.g. trim_scale(8.4100) → 8.41

R. Du
  • 544
  • 4
  • 16
-1

As per w3resource-http://www.w3resource.com/PostgreSQL/trunc-function.php

The PostgreSQL trunc() function is used to truncate a number to a particular decimal places. If no decimal places are provided it truncate toward zero(0).

Example 1: PostgreSQL TRUNC() function

SELECT TRUNC(123.4562) AS "Truncate";
Output : Truncate
'---------
123
(1 row)

Example 2: PostgreSQL TRUNC() function

SELECT TRUNC(67.4562,1) AS "Truncate upto 1 decimal";
Output : Truncate
'---------
123.4
(1 row)

Example 3: PostgreSQL TRUNC() function

SELECT TRUNC(67.4562,2) AS "Truncate upto 2 decimal";
Output : Truncate
'---------
123.45
(1 row)

So, second value after comma is values to show after decimal.

Kimchy
  • 501
  • 8
  • 24
  • I think you are missing the point of the question. And you should not paste an image that is obviously not yours without linking to the source - or not at all if it isn't free or you don't hold the necessary rights. – Erwin Brandstetter Aug 18 '17 at 00:07
  • Good, the main issue is fixed. I still think the question is asking for something else than what you answer. Basic functionality of `trunc()` is clear, trailing '0' in the display of the resulting `numeric` value are the problem. – Erwin Brandstetter Aug 18 '17 at 11:24