BigDecimal with smart precision (scale) [duplicate] - java

This question already has answers here:
Removing trailing zeros from BigDecimal in Java
(6 answers)
Closed 5 years ago.
How to set generic scaling according to decimal roundness as a number
BigDecimal num = new BigDecimal(25)
BigDecimal result = num.divide(new BigDecimal(13), 7, RoundingMode.HALF_EVEN)
// 1.9230769
but
BigDecimal result = num.divide(new BigDecimal(10), 7, RoundingMode.HALF_EVEN)
// 2.5000000 that should be 2.5
Probably analyzing remainder?
Is there something in the API for such case?

Look At https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#stripTrailingZeros--
BigDecimal bd = new BigDecimal("235.000");
System.out.println( bd.stripTrailingZeros() );

Related

BigDecimal and rounding [duplicate]

This question already has answers here:
Rounding Bigdecimal values with 2 Decimal Places
(5 answers)
Closed 2 years ago.
I have been looking for answers here re: rounding and BigDecimal, but I am having trouble. Can someone help?
The actual result of the below division is 11.469...
BigDecimal a = new BigDecimal(0.32);
BigDecimal b = new BigDecimal(2.79);
BigDecimal diffPercent = (a.divide(b, 2, RoundingMode.HALF_EVEN)).multiply(HUNDRED); // 11.00
BigDecimal diffPercent = (a.divide(b, 4, RoundingMode.HALF_EVEN)).multiply(HUNDRED); // 11.4700
How can I get 11.47 (two decimal places)?
Instead of multiplying by BigDecimal(100), move the decimal point to the right:
BigDecimal diffPercent = (a.divide(b, 4, RoundingMode.HALF_EVEN)).movePointRight(2)
Output: 11.47
This works because moving the decimal point only adjusts the scale of the BigDecimal.
BigDecimal bg = new BigDecimal("11.468");
MathContext mc = new MathContext(3); // 3 precision
// bg1 is rounded using mc
final BigDecimal round = bg.round(mc, RoundingMode.CEILING);
System.out.println(round);
Posting as this is another example of how to round

Wrong result when calculating floats in Eclipse [duplicate]

This question already has answers here:
Retain precision with double in Java
(24 answers)
Is floating point math broken?
(31 answers)
Closed 4 years ago.
Eclipse gives the wrong result when trying to calculate the sum of two floats.
In my code, there are 2 float variables: float from = 0.025 and float to = 1.
Then result has double variable: double value = 7 * from / to.
Eclipse compiler shows: value = 0.174999997019767760
In excel calculator, this result was value = 0.175
How can I solve this an issue?
This is simply due to your Java program not rounding the result the same way the excel calculator does. This is a result of the way computers handle floating point arithmetic. You have two options: round the result, or use the java BigDecimal class. If you want to round the result, you can use:
float from = 0.025f;
float to = 1;
double value = 7 * from / to;
DecimalFormat ds = new DecimalFormat("#.###");
double rounded = Double.parseDouble(ds.format(value));
System.out.println(rounded);
If you would rather not have to round, you can use the BigDecimal class.
Java BigDecimal

Calculate percentage with BigDecimals [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 4 years ago.
I want to calculate the of a number I receive in BigDecimal format :
BigDecimal number1 = new BigDecimal("17");
int percentage = 50;
BigDecimal percentageAmount = number1.multiply(new BigDecimal(percentage/100));
but I got a 0 !
Cast the divided result to double. The integer division is returning zero as expected. This should work.
BigDecimal percentageAmount = number1.multiply(new BigDecimal((double)percentage/100));
Or, make the 100 to 100.0.
BigDecimal percentageAmount = number1.multiply(new BigDecimal(percentage/100.0));
These solutions would work if the number is small as you have used. But these solutions won't give the precise results when the number is big. This would be the best approach for avoiding the precision error:
BigDecimal percentageAmount = number1.multiply(BigDecimal.valueOf((double)percentage/100));

Java Android stop getting recurring decimal number [duplicate]

This question already has answers here:
round up to 2 decimal places in java? [duplicate]
(12 answers)
Closed 5 years ago.
I'm doing a calculation on android studio (java) and the answer that I get back is like 4.654783632444251. I don't want the long answers. Preferably Two Decimal places would be ideal. Using Double.parseDouble
You can round it to two decimal places:
Math.round(myNumber * 100) / 100
Or you can format it using
String.format("%.2f", myNumber)
The second method even prints the decimal places if they are 0.
protected double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
Just call this method entering your value and 2 places.

Multiplication of java float variable [duplicate]

This question already has answers here:
Double calculation producing odd result [duplicate]
(3 answers)
Closed 7 years ago.
I have a piece of code as below.
String wtf = "8.40";
float ft = Float.parseFloat(wtf); //8.4
ft *= 100.0F;
the value of "ft" above is coming as 839.99994
I expected output as 840.00000
How can I correct my code so that it gives me 840.00000 as output
String wtf = "8.40";
double ft = Double.parseDouble(wtf); //8.4
ft *= 100.0F;
There's is a lot of rounding when you use float. This is why you get this "unexpected" value. And I use parseDouble because the cost is less.
I suggest using BigDecimal for arithmetic and calculations.
BigDecimal BDa = new BigDecimal("8.40");
BigDecimal BDc = BDa.multiply(new BigDecimal("100.0"));
Use Double.valueOf(wtf) instead of Float.parseFloat(wtf)

Categories