• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

double value changes

 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI
I have a double variable which has the value

I am passing this to a Stored Procedure using the doublebind.
When i print the value before and after bind it prints
20.54
but when i print the value in the stored procedure, the value printed is
20.539999999999999 and it causes some functionality to fail.
I am confused on how a value which is visible as a two decimal number gets converted to such a big number.
Mind you, this happens for some numbers only, other one is 13.03.
The bind variable in the Stored procedure is of type Number.
Please help me out anybody, need solution and explanation for this.

Thanks.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to understand something about the way computer store floating point values.

A double does not have infinite precision, so some numbers cannot be represented 100% accurately. The following article explains it in detail, but it's a bit technical: What Every Computer Scientist Should Know About Floating-Point Arithmetic.

If you really need infinite precision, consider using java.math.BigDecimal to store your numbers. In particular if your software is working with amounts of money, you should use this, because rounding errors are most of the time unacceptable, even if it is just about a few cents.

Otherwise, you can use something like String.format(...) to format your numbers properly before displaying them on screen.
 
Niki Nono
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply but it turned out to be an imcompatible database driver problem.
It seems that Oracle Client 10.1.0 does not work correctly with Oracle Server 10.2.0.
Well something new to learn everyday.
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Niki Nono:
HI
I have a double variable which has the value

I am passing this to a Stored Procedure using the doublebind.
When i print the value before and after bind it prints
20.54
but when i print the value in the stored procedure, the value printed is
20.539999999999999 and it causes some functionality to fail.
I am confused on how a value which is visible as a two decimal number gets converted to such a big number.
Mind you, this happens for some numbers only, other one is 13.03.
The bind variable in the Stored procedure is of type Number.
Please help me out anybody, need solution and explanation for this.

Thanks.




Hi

The problem is very common in data base problems.

Ofcourse the problem will be very worse if you move to maintain a precision of <30Digits>.<15Digits>. Then what you will do for that?

the best solution for that is use the BigDecimal Class.

It is very useful even for small numbers also to maintain the precision.

thanks
 
Who knew that furniture could be so violent? Put this tiny ad out there to see what happens:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic