java - compare BigInteger with BigDecimal [duplicate] - java

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)));

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 is the difference between int parseInt(String s) and Integer.valueOf(String s)? Also What is radix with a numerical parameter {10 or etc} [duplicate]

This question already has answers here:
Different between parseInt() and valueOf() in java?
(11 answers)
Integer.valueOf() vs. Integer.parseInt() [duplicate]
(5 answers)
What is the radix parameter in Java, and how does it work?
(6 answers)
Closed 3 years ago.
I have used these two in Java and BlueJ but I am not sure where the difference between these two lies. Also in my book in the description given for parseInt there is a mention of radix 10. What exactly is radix 10?
The two methods are almost the same in terms of logic.
The difference is that valueOf returns an Integer and parseInt returns an int.
You can see that both call parseInt(s, 10), which means they treat the input String as a number of radix (base) 10 (i.e. a decimal number).
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}

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

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

Value equation of wrapper classes in Java [duplicate]

This question already has answers here:
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
Comparing boxed Long values 127 and 128
(4 answers)
Closed 5 years ago.
I'm trying to compare 4 values in a program.
Long val1 = 127l;
Long val2 = 127l;
Long val3 = 128l;
Long val4 = 128l;
Log.e("XXXX",(val1==val2)+" "+(val3==val4));
This gives me val1l == val2l is true and val3' == val4l is false. What is the reason behind this output. I think it's coming because of the wrapper class. Please help me to clear this.

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);

Categories