java


BigDecimal adding wrong value


I have a BigDecimal defined like this:
private static final BigDecimal sd = new BigDecimal(0.7d);
if i print it, i get the value:
0.6999999999999999555910790149937383830547332763671875
which causes some wrong calculations. Does anyone know a way to get the exact value of 0.7 as BigDecimal? Change it to 0.71 would view the right result, but it shouldn't be like that
Use a String literal:
private static final BigDecimal sd = new BigDecimal("0.7");
If you use a double, actually public BigDecimal(double val) is called. The reason you do not get 0.7 is that it cannot be exactly represented by a double. See the linked JavaDoc for more information.
You should use the declared value in String literal such as new BigDecimal("0.7");
Here are three ways:
private static final BigDecimal sd = new BigDecimal("0.7");
private static final BigDecimal sd = new BigDecimal(0.7d, MathContext.DECIMAL32);
private static final BigDecimal sd = new BigDecimal(0.7d, MathContext.DECIMAL64)
Perhaps if you bothered to read the documentation, i.e. the javadoc of the constructor you're using, you'd already know the answer.
When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.
When you then look at the javadoc of BigDecimal.valueOf(double), you'll find:
Note: This is generally the preferred way to convert a double (or float) into a BigDecimal, as the value returned is equal to that resulting from constructing a BigDecimal from the result of using Double.toString(double).
So there is your answer: Use BigDecimal.valueOf(0.7d), not new BigDecimal(0.7d).

Related Links

how to make multiple war artifacts with gradle source sets
why concurrent mode failure when there were still enough memory in old memory
JavaFX Pong Vertical Game issues
java.lang.UnsatisfiedLinkError while loading libRETS jar
excel in java, how to use one column of data to print out another?
How to find WebElement in a List<WebElement> by text?
How to create attribute converter for #id
OSB fn:bea Function using XQuery Engine in Java
How to subtract days from ZonedDateTime [duplicate]
Log Correlation ID with Vertx
TreeViewer doesn't show input
Unexpected behavior of selenium webdriver
Tomcat no more stopping down on error following Spring Boot migration
How to add authentication to Elastic Transport Client
Using Serialization to save and read files in Java
EJB - practical examples of passivation and activation

Categories

HOME
openlayers
osgi
blogger
magnific-popup
view
webstorm
adb
malloc
rdf
win32gui
umd
blueprintjs
wamp
bs4
maude-system
reveal.js
etl
correlation
gz
task
ibm-odm
hapi
django-admin
tostring
windows-7-x64
connection-string
contextmenu
custom-wordpress-pages
language-agnostic
tibco-mdm
dxf
dynamic-featured-image
kannel
libuv
vision
c++-amp
bcd
ioio
copying
typed.js
pim
mapdb
bower-install
plsql-psp
io-redirection
disassembling
scaffold
c11
webix-treetable
tasker
mesos-chronos
email-templates
ibpy
midl
websphere-mq-fte
apple-news
logfiles
osx-mavericks
rails-routing
boost-multi-index
togetherjs
git-diff
typed-lambda-calculus
epson
synchronous
migradoc
instant
login-control
whois
livequery
phpcas
collapse
muse
map-projections
ideamart
interrupted-exception
lustre
bigbluebutton
facebook-graph-api-v2.4
coldbox
file-copying
fpml
xcode-6.2
contenttype
stxxl
gnu-smalltalk
cdc
android-screen-support
ember-app-kit
clipper
e4x
browser-detection
first-responder
chrono
spring-portlet-mvc
removeclass
eclipse-templates
whoosh
iphone-web-app
lang
yslow
sudzc
getresponsestream
preference
audio-capture
eqatec
simpletest
nerddinner
grid-system
uiq3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App