• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

BigDecimal abs not return exactly value  RSS feed

 
Ranch Foreman
Posts: 111
1
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to remove the negative sign value

But I get 0.2059999999999999886757251488234032876789569854736328125 instead of 0.206. I though abs will returns a BigDecimal whose value is the absolute value of this BigDecimal, no ?
Can someone explain ?
 
Bartender
Posts: 2180
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can find the answer is in the javadoc of the constructor.
It is not a problem with the abs() method. It is a problem with the instance of BigDecimal that you are creating.
 
Saloon Keeper
Posts: 9248
177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To further clarify what Paweł wrote, the value you are using to create the BigDecimal instance with is not actually -0.206, but the closest value that can be represented by the double datatype.
 
Paweł Baczyński
Bartender
Posts: 2180
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:To further clarify what Paweł wrote, the value you are using to create the BigDecimal instance with is not actually -0.206, but the closest value that can be represented by the double datatype.


And this value is exactly -0.2059999999999999886757251488234032876789569854736328125.
 
Marshal
Posts: 60155
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:You can find the answer is in the javadoc of the constructor. . . . .

You will also find the solution to correct your problem. Alternatively, try its valueOf() method.
 
Randy Tong
Ranch Foreman
Posts: 111
1
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all 
 
Campbell Ritchie
Marshal
Posts: 60155
188
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!