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.
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
When I divided the large number into a small number then the division is correct but when I write the small number to divide a large number the answer returns wrong. In my scenario, the small number always be first. here is my code this code return 7.4074074074074075E-6 but the correct result is 0.0000074074.
double itf = 0.0;
double a = 4.0;
double b = 540000;
itf = a / b;
Log.i(TAG, "savedata: outputvalue=" + itf);
BigDecimal a = new BigDecimal("4");
BigDecimal b = new BigDecimal("540000");
// 0.0000074074
a.divide(b, MathContext.DECIMAL128);
You should use a decimal type. double is outside the scope of support
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
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));
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 4 years ago.
I have this float 98.01645 and I'm getting 98.02 with this function:
String lastCredit = String.format("%.2f", AppSingleton.getInstance().credit);
That I want is get 98.01 (only two decimals or not rounded number). I'm trying but I can't get the way to do it work.
Doing it manually:
String lastCredit = String.format("%.2f", java.lang.Math.floor(100.0*AppSingleton.getInstance().credit)*0.01);
Multiplying by 100.0 to move the decimal point two to the right, then rounding down, then moving the decimal point two to the left by multiplying with 0.01.
You can try this
String lastCredit = String.format("%.2f",Math.floor((98.01645 * 100)) / 100);
System.out.println(lastCredit);
You basically multiply the value by 100 because you need 2 numbers after the decimal and round down that value. After you have the result you divide it by 100 again.
I would not call this elegant, but you can just use String.format to get 3 decimal places and remove the last using substring:
String lastCredit = String.format("%.3f", AppSingleton.getInstance().credit);
lastCredit = lastCredit.substring(0, lastCredit.length() - 1);
Use below code instead of String lastCredit = String.format("%.2f", AppSingleton.getInstance().credit);
DecimalFormat decimalFormat = new DecimalFormat("#.##");
decimalFormat.setRoundingMode(RoundingMode.FLOOR);
String lastCredit = decimalFormat.format(f);
This question already has answers here:
Round a double to 2 decimal places [duplicate]
(13 answers)
Closed 9 years ago.
I have a Double value Double val = 49.569632
How can I roundup the val to get 49.57
You can use the DecimalFormat.
double d = 4.569632;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
Or you can use the below method as mentioned in this answer as Luiggi Mendoza suggested.
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
}
A simple way to round is when printing
double val = 49.569632; // this should be a primitive, not an object
System.out.printf("%.2f%n", val);
or you can round the value first
double rounded = Math.round(val * 1e2) / 1e2;
System.out.println(rounded);
IMHO Using BigDecimal is slower, more complicated to write and no less error prone than using double if you know what you are doing. I know many developer prefer to use a library than write code themselves. ;)