2

I want to divide values and round them up to 8 decimal places but i found that some divisions return in scientific notation.

How can i always get round division without scientific notation?

  • select round( 123/100000000::decimal, 8 )

    returns 0.00000123 as expected.

  • select round( 1/100000000::decimal, 8 )

    returns 1e-8 but... i was expecting 0.00000001

How can i round 1/100000000 to 8 decimal places and return 0.00000001 ?

sql fiddle: http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/1534

Thanks for help. best,

user1221679
  • 625
  • 1
  • 9
  • 16

2 Answers2

6

Actually, when I try your code in Postgres 9.3.4 using pgAdmin, both return the values you want. The values are not in exponential notation. Hence, I suspect this is an issue with your application, not the database.

An easy way to check is to put the value as a string:

select round( 1/100000000::decimal, 8 )::text

This should not return exponential notation.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • you are right, casting as text returns value not in exponential/scientific notation. For just displaying value (instead of calculations) this is what i need. Here goes updated sqlfiddle demo: http://sqlfiddle.com/#!15/9eecb7db59d16c80417c72d1e1f4fbf1/1550 – user1221679 Jul 11 '15 at 14:01
0

Probably second case go beyond precision range 8.

You can check this Question Selecting floating point numbers in decimal form

Also what version are you working with because in pgAdmin 9.2 I get a different result

enter image description here

Community
  • 1
  • 1
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118