This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I am using this JAVA program where I am multiplying a double with a double and storing it in a double.
My expected output is 7.14 as per my mathematics knowledge, but I get 7.140000000000001. I don't understand why is the 0000000000001 part is coming
public class Main
{
public static void main(String[] args) {
double a=7.0;
double b=1;
double c=a*b*1.02;
System.out.println(c);
}
}
So you are getting a super high level precision output for your operation. If you want to set a precision limit, you can do so following the example below
double no=12.786;
DecimalFormat dec = new DecimalFormat("#0.00");
System.out.println(dec.format(no));
Related
This question already has answers here:
Round a double to 2 decimal places [duplicate]
(13 answers)
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 5 years ago.
I have an application where I intend to add two double type values. In some certain situation, the result contains a very long decimal part. I was able to reproduce the problem in a separate program.
public class Test {
public static void main (String args []) {
double total1= 8.69;
double total2 = 1.69;
double totalamount = total1 + total2 ;
System.out.println(totalamount);
}
}
Output: 10.379999160766602 (The expexted output should be in the format of #.##)
I have tried changing the values of total1 and total2. The output is in expected format in some cases. The occurrence of the long values is very random and it goes with the float datatype as well.
What is the explanation for the above behavior?
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 6 years ago.
Every time I run this test program it display, 0.0 instead of 0.5
Does anyone know how to fix this in Eclipse?
public class Test {
public static void main(String[]args){
double distance;
distance = 1/2;
System.out.println(distance);
}
}
Your division is Integer division. There is no fault of Eclipse. Try following:
distance = 1/2.0;
OR
distance = 1.0/2;
Output:0.5
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
How to Round Decimals to 2 Places after the Decimal Point (Java)
(4 answers)
Closed 7 years ago.
I'm new to java programming. I would like to round up a price to the nearest 2 decimal places with a multiple of 5.
Eg.
38.80 stays the same.
38.81 to 38.80.
38.82 to 38.80.
38.83 to 38.85.
38.84 to 38.85.
38.85 stays the same.
38.86 to 38.85.
38.87 to 38.85.
38.88 to 38.90.
38.89 to 38.90.
38.90 stays the same.
I tried the provided duplicates but they come out only to 1 decimal place.
Eg. 38.82 to 38.8.
This is my code:
import java.text.DecimalFormat;
public class RoundUp {
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("#.##");
double num = 38.84;
System.out.println(df.format(Math.round(num*10.00)/10.00));
}
}
I have looked into other model answers by experts in this web but none of them really answer my question. Setting into 2 decimal places, I'm using DemicalFormat. That I know, but rounding the number, let's say 38.83 to 38.85 and 38.87 to 38.90 is what I really want.
It is a rounding system that my country is using. Can check it out here.
**
This question has been answer by #Mureinik double rounded =
Math.round(num * 100.0 / 5.0) * 5.0 / 100.0;
**
I would recommend you to use BigDecimal instead of double when you are dealing with money.
And then it would be like
BigDecimal value = new BigDecimal(38.84);
value = value.setScale(2, BigDecimal.ROUND_HALF_UP)
You can refer Javadocs for ROUND_HALF_UP and setScale
This question already has answers here:
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
(14 answers)
Floating point arithmetic not producing exact results [duplicate]
(7 answers)
Closed 8 years ago.
I am having problems with the java double data type. The problem is that for some cases the result obtained is very large number of digits after the decimal point. the same calculation done on the calculator, manually hardly reached 2 digits after decimal.
the code is as follows:
public class Calculater {
public static void main(String[] args) {
// TODO Auto-generated method stub
String quantityInString="700g";
int indexOfg=quantityInString.indexOf("g");
String onlyQuantityInString=quantityInString.substring(0, indexOfg);
int onlyQuantityInInt=Integer.parseInt(onlyQuantityInString);
double perUnitCostOfThisItem=29.00;
double returnFloat=0;
returnFloat=(onlyQuantityInInt/(1000.00))*perUnitCostOfThisItem;
System.out.println("returnFloat="+returnFloat);
}
}
The program output is: returnFloat=20.299999999999997
The answer using a calculator is 20.3
I have no idea why this is happening?
I have tried it in an eclipse running on laptop and also on an Android phone
both show same result.
Try this:
float finalValue = Math.round( value * 100.0 ) / 100.0;
Try this for precise calculations. BigDecimal is more accurate than double.
String quantityInString="700g";
int indexOfg=quantityInString.indexOf("g");
String onlyQuantityInString=quantityInString.substring(0, indexOfg);
BigDecimal onlyQuantityInInt= new BigDecimal(onlyQuantityInString);
BigDecimal perUnitCostOfThisItem= new BigDecimal("29.00");
BigDecimal returnFloat= new BigDecimal(onlyQuantityInString);
returnFloat = returnFloat.divide(new BigDecimal("1000"));
returnFloat = returnFloat.multiply(perUnitCostOfThisItem);
System.out.println("returnFloat="+returnFloat);
If you need precise calculations BigDecimal should be used rather than float and double.
The output for this code is: returnFloat=20.300
If you need to know the reason why double acts like that the look at this
To format your double you can do it by
String.format("%.2f", floatValue);
Note this formats it till 2 places.
You can also use DecimalFormat. One way to use it:
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(decimalNumber));
Another one is to construct it using the #.## format.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Round a double to 2 significant figures after decimal point
The code below works
import java.text.DecimalFormat;
public class Test {
public static void main(String[] args){
double x = roundTwoDecimals(35.0000);
System.out.println(x);
}
public static double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.00");
twoDForm.setMinimumFractionDigits(2);
return Double.valueOf(twoDForm.format(d));
}
}
it results to 35.0.
How to forced the minimum number of decimal places?
The output I want is 35.00
This isn't working like you expect because the return value of roundTwoDecimals() is of type double, which discards the formatting you do within the function. In order to achieve what you want, you might consider returning the String representation from roundTwoDecimals() instead.
Converting formatted number back to double will make you lose all formatting changes. Change the function to:
public static String roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.00");
return twoDForm.format(d);
}
EDIT: you were right, "#.00" is correct.