how to check if two doubles are equal in java [duplicate] - java

This question already has answers here:
What is good way to check equality between 2 doubles in Java
(6 answers)
Closed 3 years ago.
I am writing a program to help students practice division, how can I compare the user Input and the correct answer if they are doubles

You can convert it to BigDecimal, and compare with the required precision of decimal places
BigDecimal aa = new BigDecimal(a);
BigDecimal bb = new BigDecimal(b);
aa = aa.setScale(4, BigDecimal.ROUND_DOWN);
bb = bb.setScale(4, BigDecimal.ROUND_DOWN);
return aa.equals(bb);

Either use
Double.compare()
or
final double EPSILON = 0.00000001d;
Math.abs(firstDouble - secondDouble) < EPSILON;
it's based on level of accuracy you needed for comparison

Related

Why my function is displaying a weird result when converting string to double? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I have a very weird result when I'm using my function and I think that I'm missing something with the rounding and double in java.
For example, when I provide the value 00159,300 for number and 100 for conversion I have 15930,000000000002 which is not possible!
public static String convertMultiply(String number, String conversion) {
number=number.replace(",", ".");
BigDecimal res=BigDecimal.valueOf(Double.valueOf(number)*Integer.valueOf(conversion));
res=res.stripTrailingZeros();
return res.toPlainString().replace(".", ",");
}
thanks in advance!
Double is an approximation of decimal values in Java. Instead, replace your line using double with:
BigDecimal res = (new BigDecimal(number)).multiply(new BigDecimal(conversion));

What can I do against floating point notations Errors? [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Why not use Double or Float to represent currency?
(16 answers)
Closed 5 years ago.
I need to multiply a float by 100 in order to convert from € to cents. The problem I am having is, that the value of big numbers isn't calculated correctly.
For example:
String aa = "1000009.00";
aa = aa.replaceAll(",", ".");
float bb = Float.parseFloat(aa);
bb=Math.round(bb*100);
System.out.println(bb);
What I am getting is: 1.00000896E8
So I know this is because of the way float in Java works.
I have searched for an answer but people always say "use Math.round()" which doesn't work for me.
What can i do to prevent this?
You can use double for more precision (or BigDecimal if you expect to work with very big numbers).
String aa = "1000009.00";
double bb = Double.parseDouble(aa);
bb=Math.round(bb*100);
System.out.printf("%.2f", bb); // it prints only two digits after the decimal point
Output
100000900.00
You can use BigDecimal::multiply for example :
String aa = "1000009.00";
aa = aa.replaceAll(",", ".");
BigDecimal fullValue = new BigDecimal(aa);
System.out.println("full value = " + fullValue.multiply(new BigDecimal(100)));
Output
full value = 100000900.00

How to format a Double coming from a Object in Java? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 6 years ago.
I want to format a Double obtained from an Object so it only displays three digits after the decimal point. Here's the current code:
Three a = Data.get(index);
// Get the y-axis acceleration value
double b = a.getY();
String accelerationOutUnfiltered = Double.toString(b);
Data[0] = accelerationOutUnfiltered;
Note: I am doing this in Android, and when I use String.format("%.3f", y) this doesn't work and it throws me a error in Android Studio. Currently the above code works but it displays 15 digits after the decimal point.
I have tried several forms, but they all have failed. Please help. Thanks :)
You could use DecimalFormatter.
For example:
double b = a.getY();
DecimalFormat formatter = new DecimalFormat("#.###");
formatter.setRoundingMode(RoundingMode.CEILING);
String formattedDouble = formatter.format(b);
Have tried decimal formatter?
You do something like this:
DecimalFormat df = new DecimalFormat("#.#####");
df.format(<your_number>);

How to get exact decimal value from bigdecimal in java [duplicate]

This question already has answers here:
"new BigDecimal(13.3D)" results in imprecise "13.3000000000000007105.."?
(5 answers)
Closed 6 years ago.
I am trying to get number of digits after decimal point in BigDecimal value.
BigDecimal big = new BigDecimal(1231235612.45);
String[] str = big.toPlainString().split("\\.");
System.out.println(" Decimal Value: " + str[1]);
Using this I am getting following output -
Decimal Value: 4500000476837158203125.
Actualy I want to display only 45 as per the original BigDecimal value (1231235612.45).
So, my expected output is Decimal Value: 45.
But, while conversion it adds more digits after decimal points.
Is there any method or code to get exact same value from BigDecimal?
Don't use the double Constructor of BigDecimal (See Javadoc, it is discouraged).
use String constructor
new BigDecimal("1231235612.45");
or use MathContext
new BigDecimal(1231235612.45, MathContext.DECIMAL64);

java - compare BigInteger with BigDecimal [duplicate]

This question already has answers here:
Equals operator for zeros (BigDecimal / Double) in Java
(7 answers)
Closed 8 years ago.
I want to compare a BigInteger with a BigDecimal. For example, the integer 10 should be equal to the decimal 10.0.
Why does the following return false? I am creating a new decimal from the integer, and then comparing the two decimals.
System.out.println(new BigDecimal(BigInteger.valueOf(10)).equals(BigDecimal.valueOf(10.0)));
This returns true:
System.out.println(new BigDecimal(BigInteger.valueOf(10)).equals(BigDecimal.valueOf(10)));
How can I correctly compare a BigInteger with a BigDecimal in the mathematical/human definition (10 == 10.0)?
For BigInteger and BigDecimal you should use the compareTo method to check if their value is the same
System.out.println(new BigDecimal(BigInteger.valueOf(10)).compareTo(BigDecimal.valueOf(10.0)));

Categories