I am working on an currency converter android application in which I am trying to multiply a double with another double value but after multiplying I am not getting the correct value. i also tried BigDecimal and still didn't get the correct amount. What would be best way to multiply doubles?
second_amount = Double.parseDouble(second.getText().toString());
first_amount = Double.parseDouble(first.getText().toString());
result = first_amount * second_amount;
second.setText("" + df.format(result));
after using BigDecimal
second_amount = Double.parseDouble();
bdf = new BigDecimal(second.getText().toString());
//first_amount = Double.parseDouble(first.getText().toString());
bds = new BigDecimal(second.getText().toString());
BigDecimal bdr = bdf.multiply(bds);
//result = first_amount * second_amount;
second.setText("" + bdr);
If you are doing something like currency calculations, where exact representation of short decimal fractions matters, it is best not to involve double at all. Pass the original string representation of the number directly to the BigDecimal constructor, and do your arithmetic in BigDecimal.
For example, new BigDecimal("0.1") has the value 0.1, but new BigDecimal(0.1) has the very slightly different value 0.1000000000000000055511151231257827021181583404541015625, due to rounding on the conversion of the literal to double.
Related
double a = 10.0;
double b =3;
double c = a/b;
System.out.println(c*b);//answer = 10.0
BigDecimal bigDecimal1 = BigDecimal.valueOf(c);
BigDecimal bigDecimal2 = new BigDecimal("3");
System.out.println(bigDecimal1.multiply(bigDecimal2));//answer = 10.0000000000000005
I'm trying to make a calculator, but there's a problem with 10/3*3
I don't want to just calculate 10/3*3 this formula, I want to return this formula plus 0.2323232323232 of the float. So use the BigDecimal class. There's something wrong with it
I couldn't get the exact result, that's what I wanted 10,
Rather than get 10.0000000000000005
I believe your problem may be here
double c = a/b;
...
BigDecimal bigDecimal1 = BigDecimal.valueOf(c);
You're expecting that a double can perfectly represent 10/3, and I doubt it can
Maybe try something like this, which always represents numbers as BigDecimal
new BigDecimal("10").divide(new BigDecimal("3"))
At which point you'll notice that 10/3 is not representable as a decimal
Non-terminating decimal expansion; no exact representable decimal result
You need to decide how much precision you want, and then use rounding
new BigDecimal("10")
.setScale(10)
.divide(new BigDecimal("3"), BigDecimal.ROUND_HALF_EVEN)
Or you could use a rational number library, as suggested by Patricia. Perhaps see Is there a commonly used rational numbers library in Java?
I have a question regarding floating point arithmetic in java and its precision. I did do my research on here and via google and come across some solutions but am having difficulties implementing them in my design. So in Java I am making use of the BigDecimal class in getting my calculations accurate. Note that the variables are double and the values can have a precision up to 8 decimal places to the right when doing calculations. The result (precision) to display is known and thats what I'll be storing as the current value. Also, all values come in dynamically (through a method). The argument passed should be the currentValue + the step size.
public void newValue(float value) {
//Clip to valid range, can't go over minimum/max value
value = Math.max(minimumValue, Math.min(maximumValue, value));
// TODO Implement better Floating Point Arithmetic precision
MathContext mcI = new MathContext(0, RoundingMode.HALF_UP);
MathContext mcF = new MathContext(8, RoundingMode.HALF_UP);
BigDecimal valueBD = new BigDecimal(value, mcF);
BigDecimal minimumBD = new BigDecimal(minimumValue, mcF);
BigDecimal stepBD = new BigDecimal(step, mcF);
BigDecimal currentValueBD = new BigDecimal(currentValue, mcF);
BigDecimal totalStepsBD = valueBD.subtract(minimumBD, mcF);
//Ensure value is divisible by stepsize
totalStepsBD = totalStepsBD.divide(stepBD, mcI);
valueBD = stepBD.multiply(totalStepsBD, mcF);
valueBD = valueBD.add(minimumBD, mcF);
// arithmetic without using BigDecimal (old)
//int totalSteps = (int) ((value- minimumValue)/ step);
//value = totalSteps * step + minimumValue;
if(!(valueBD.equals(currentValueBD))) {
valueBD = valueBD.setScale(displayPrecision, RoundingMode.HALF_UP);
currentValue = valueBD.floatValue();
dispatch();
}
}
Now, it works with some values but not all. Especially when I mess with the step size. So if step = 0.1 it was fine. If I made it 0.005, I'd get an AirthmeticException - non terminating decimal expansion on the step where
totalStepsBD = totalStepsBD.divide(stepBD, mcI);
When .005 is set for the step variable, after making a BigDeciaml (stepBD) it comes out to .0049999999... Not sure if that helps but if you have any ideas please let me know. Thank you.
Pass a String step (and a String value) to the BigDecimal constructor. You can't precisely represent 0.005 as a double (or a float).
BigDecimal stepBD = new BigDecimal("0.005"); // <-- works as a `String`.
Edit
Or as noted below, use BigDecimal.valueOf(double)
BigDecimal stepBD = BigDecimal.valueOf(0.005);
In Java, I want to take a double value and convert it to a BigDecimal and print out its String value to a certain precision.
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
double d=-.00012;
System.out.println(d+""); //This prints -1.2E-4
double c=47.48000;
BigDecimal b = new BigDecimal(c);
System.out.println(b.toString());
//This prints 47.47999999999999687361196265555918216705322265625
}
}
It prints this huge thing:
47.47999999999999687361196265555918216705322265625
and not
47.48
The reason I'm doing the BigDecimal conversion is sometimes the double value will contain a lot of decimal places (i.e. -.000012) and the when converting the double to a String will produce scientific notation -1.2E-4. I want to store the String value in non-scientific notation.
I want to have BigDecimal always have two units of precision like this: "47.48". Can BigDecimal restrict precision on conversion to string?
The reason of such behaviour is that the string that is printed is the exact value - probably not what you expected, but that's the real value stored in memory - it's just a limitation of floating point representation.
According to javadoc, BigDecimal(double val) constructor behaviour can be unexpected if you don't take into consideration this limitation:
The results of this constructor can be somewhat unpredictable. One
might assume that writing new BigDecimal(0.1) in Java creates a
BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with
a scale of 1), but it is actually equal to
0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that
matter, as a binary fraction of any finite length). Thus, the value
that is being passed in to the constructor is not exactly equal to
0.1, appearances notwithstanding.
So in your case, instead of using
double val = 77.48;
new BigDecimal(val);
use
BigDecimal.valueOf(val);
Value that is returned by BigDecimal.valueOf is equal to that resulting from invocation of Double.toString(double).
It prints 47.48000 if you use another MathContext:
BigDecimal b = new BigDecimal(d, MathContext.DECIMAL64);
Just pick the context you need.
You want to try String.format("%f", d), which will print your double in decimal notation. Don't use BigDecimal at all.
Regarding the precision issue: You are first storing 47.48 in the double c, then making a new BigDecimal from that double. The loss of precision is in assigning to c. You could do
BigDecimal b = new BigDecimal("47.48")
to avoid losing any precision.
Why not :
b = b.setScale(2, RoundingMode.HALF_UP);
It's printing out the actual, exact value of the double.
Double.toString(), which converts doubles to Strings, does not print the exact decimal value of the input -- if x is your double value, it prints out exactly enough digits that x is the closest double to the value it printed.
The point is that there is no such double as 47.48 exactly. Doubles store values as binary fractions, not as decimals, so it can't store exact decimal values. (That's what BigDecimal is for!)
The String.format syntax helps us convert doubles and BigDecimals to strings of whatever precision.
This java code:
double dennis = 0.00000008880000d;
System.out.println(dennis);
System.out.println(String.format("%.7f", dennis));
System.out.println(String.format("%.9f", new BigDecimal(dennis)));
System.out.println(String.format("%.19f", new BigDecimal(dennis)));
Prints:
8.88E-8
0.0000001
0.000000089
0.0000000888000000000
BigDecimal b = new BigDecimal(c).setScale(2,BigDecimal.ROUND_HALF_UP);
In Java 9 the following is deprecated:
BigDecimal.valueOf(d).setScale(2, BigDecimal.ROUND_HALF_UP);
instead use:
BigDecimal.valueOf(d).setScale(2, RoundingMode.HALF_UP);
Example:
double d = 47.48111;
System.out.println(BigDecimal.valueOf(d)); //Prints: 47.48111
BigDecimal bigDecimal = BigDecimal.valueOf(d).setScale(2, RoundingMode.HALF_UP);
System.out.println(bigDecimal); //Prints: 47.48
I'm looking at some code that was using a double variable to store the results of (360-359.9998779296875) which is 0.0001220703125. The double variable stores this as -1.220703125E-4. When I use a BigDecimal its stored as 0.0001220703125. Why does double store it as -1.220703125E-4?
I won't mention precision issues here but only the way the numbers get printed.
As explained in the javadoc of Double#toString:
If m is less than 10^-3 or greater than or equal to 10^7, then it is represented in so-called "computerized scientific notation."
Whereas the javadoc of BigDecimal#toString:
If the scale is greater than or equal to zero and the adjusted exponent is greater than or equal to -6, the number will be converted to a character form without using exponential notation
You can try this little program to check the various output formats and, in particular, the threshold at which the representation switches from standard notation to scientific notation is not the same in the two classes.
Output:
0.5 0.5
3.0517578125E-5 0.000030517578125
9.5367431640625E-7 9.5367431640625E-7
Code:
public static void main(String args[]) {
//trying to use numbers with an exact double representation
double d1 = 0.5;
double d2 = 0.000030517578125;
double d3 = 0.00000095367431640625;
BigDecimal bd1 = new BigDecimal(d1);
BigDecimal bd2 = new BigDecimal(d2);
BigDecimal bd3 = new BigDecimal(d3);
System.out.println(d1 + "\t\t\t" + bd1);
System.out.println(d2 + "\t\t" + bd2);
System.out.println(d3 + "\t" + bd3);
}
Both are same. Whatever tool/IDE you are using is displaying them in different formats.
http://epramono.blogspot.com/2005/01/double-vs-bigdecimal.html
The underlying data structures to store these are different (from my recollection floating point can have a broader range of values, but BigDecimal has more accuracy), but what you're actually seeing is just a different way of outputting the same number. I would assume it exists this way because of the larger range of floating point; outputting absurdly large numbers sans scientific notation is just obnoxious, so I believe scientific notation is the default.
I would like to convert a possibly Decimal value prefixed with currency symbol into only numeric value.
For example -
The value can be like any of the following
String s1 = "£32,847,676.65";
String s2 = "£3,456.00";
String s3 = "£831,209";
I would like the result after conversion to be like - 32847676.65, 3456.00 and 831209.
I tried using the parse() method of the NumberFormat in this way -
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.UK);
numberFormat.setMinimumFractionDigits(2);
Number num = nf.parse(s1);
double dd = num.doubleValue();
BigDecimal gg = new BigDecimal(dd);
System.out.println(gg);
But the result is - 32847676.649999998509883880615234375 which is not quite exactly the correct one.
I need it to be numeric so that may be I can perform some kind of calculation.
Can you guys guide me with what else can I try
You already parse the value correctly. The problem is this:
BigDecimal gg = new BigDecimal(dd);
You covnert the value to BigDecimal, and the rounding problems of doubles account for the decimal places after the dot. Use:
BigDecimal gg = new BigDecimal(dd).setScale(2);
or
BigDecimal gg = new BigDecimal(dd).setScale(2,RoundingMode.HALF_UP);
When playing with BigDecimal, the appropriate constructor is BigDecimal(String val)
NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.UK);
BigDecimal gg = new BigDecimal(nf.parse(s1).toString());
System.out.println(gg);
BigDecimal(double val) does construct an exact decimal representation of the double value, which is not the human readable value you expected.
"The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
[...]
Therefore, it is generally recommended that the String constructor be used in preference to this one"
Source : BigDecimal javadoc
You can try the following without BigDecimal or NumberFormat. ;)
String s1 = "£32,847,676.65";
// remove the £ and ,
String s2 = s1.replaceAll("[£,]", "");
// then turn into a double
double d = Double.parseDouble(s2);
// and round up to two decimal places.
double value = (long) (d * 100 + 0.5) / 100.0;
System.out.printf("%.2f%n", value);
prints
32847676.65
If youw ant to avoid rounding error in your calculations but don't want the heavy weight BigDecimal you can use long cents.
// value in cents as an integer.
long value = (long) (d * 100 + 0.5);
// perform some calculations on value here
System.out.printf("%.2f%n", value / 100.0);
It is not guaranteed to work, but according to the NumberFormat API documentation, its getXyzInstance methods will return a DecimalFormat instance for "the vast majority of locales". This can probably be interpreted as "for all locales, unless proprietary locale service providers are installed".
If you can cast your NumberFormat to DecimalFormat, you can tell it to parse to a BigDecimal directly, reducing your code to:
DecimalFormat nf = (DecimalFormat) NumberFormat.getCurrencyInstance(Locale.UK);
nf.setParseBigDecimal(true);
BigDecimal gg = (BigDecimal) nf.parse(s1);
System.out.println(gg);
In this case, you will have no problem with the inaccuracy of binary floating point numbers.