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

ActiveMQ + MQTT + subscribe to “ActiveMQ.Advisory.Connection”
SWT CLabel vertical color gradient
$project is not working in mongo query [duplicate]
SWT CCOMBO not reposnding correctly to accessibility events(Screen Reader)
fetching data from ArrayList [duplicate]
Unable to run Unix Commands using ProcessBuilder
BroadcastReceiver comparing action names
Issues scheduling alarms when getting the time to set the AlarmManger for using a TimePicker because of AM/PM
Asking user input for an integer in java
Handle Toggle States in onResume() using Intent and SharedPreferences
Correct approach to secure the back end service - oAuth
Passing generic event to Guava EventBus?
Java Casting in Method Overloading
Call a java web service method from java client (HTTPPost)
How do you change the label text in java using eclipse and scene builder?
How to retrieve field values by passing field name using JPOS ISO8583 message format

Categories

HOME
pandas
testing
openlayers
minimum-spanning-tree
onedrive
relay
webstorm
infragistics
windows-server
ravendb
fancybox
directx
node-notifier
esper
volttron
mapserver
windows-server-2012
systemc
telephony
openrefine
fatal-error
reactive-cocoa
evopdf
contextmenu
scichart
swingx
custom-wordpress-pages
tar
captiveportal
blazemeter
vlsi
sparse-matrix
entitlements
fopen
web-api-testing
neo4j-spatial
nat
lightswitch-2013
galsim
functor
siesta-swift
y86
bitbucket-pipelines
xcode-extension
neuroscience
uft-api
webdriver-manager
opshub
user-controls
broadcastreceiver
gzipstream
apache-fop
strptime
rails-routing
paxos
veracode
integrity
yii2-extension
csound
mediaelement
grails-tomcat-plugin
removechild
httplistener
pcf
connect-by
nsviewcontroller
hill-climbing
fadeout
asp.net5
cloudbees
prerequisites
jms2
system32
dundas
apache-commons-net
dia
ng-animate
google-style-guide
sgen
ms-project-server-2010
ora-00911
robotics-studio
unc
libstdc++
isnullorempty
gnu-prolog
spyware
servicehost
floating
vc90
adrotator
putchar
spec#
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