Reputation: 1787
I've read a bunch of questions on rounding numbers to n amount of decimal places and then I've found out using BigDecimal
is pretty good
However I'm having a bit of trouble with the following exercise in my book, Consider the following input and output
Enter two floating-point numbers:
2.0
1.99998
//output
They are the same when rounded to two decimal places.
They differ by less than 0.01.
Enter two floating-point numbers:
0.999
0.991
//output
They are different when rounded to two decimal places.
They differ by less than 0.01.
MY CODE
public class test{
public static void main(String [] args ){
Scanner getNum = new Scanner(System.in);
Scanner getNumTwo = new Scanner(System.in);
BigDecimal a = getNum.nextBigDecimal();
float b = getNum.nextFloat();
BigDecimal valBNew = new BigDecimal(b).setScale(2,BigDecimal.ROUND_HALF_UP);
BigDecimal valANew = a;
System.out.println(valBNew); // 2.00
System.out.println(a); //2.0
if (valBNew == a){
System.out.println("They are the same when rounded to two decimal places");
}
else{
System.out.println("They are different when rounded to two decimal places");
}
}
}
Heres my attempt. I'm having trouble using two floats and then using big decimal. And then i'm having issues with my if statement since 2.0
isnt the same as 2.00
.
Whats my best option here? I'd like to get the rounding part working then the difference will be easy.
Upvotes: 1
Views: 617
Reputation: 7166
Based on @cricket_007 -s answer, this is how a clean solution would look like:
public class Test {
public static void main(String [] args ) {
try (Scanner getNum = new Scanner(System.in)) { // try-with-resources, e.g. it auto-closes the scanner
double a = Double.parseDouble(getNum.next()); // parsing a number from a line
double b = Double.parseDouble(getNum.next());
BigDecimal valBNew = new BigDecimal(b); // 2.00, no rounding
BigDecimal valANew = new BigDecimal(a); // 2.0
if (valBNew.compareTo(valANew) == 0) { // this is the magic
System.out.println("They are the same");
} else {
System.out.println("They are different");
}
}
}
}
Upvotes: 1
Reputation: 191728
From the BigDecimal documentation.
public int compareTo(BigDecimal val)
Compares this
BigDecimal
with the specifiedBigDecimal
. TwoBigDecimal
objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method.
Whereas, for .equals
public boolean equals(Object x)
Compares this
BigDecimal
with the specifiedObject
for equality. UnlikecompareTo
, this method considers twoBigDecimal
objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).
Therefore, this is correct
if (valBNew.compareTo(a) == 0) {
}
Upvotes: 3
Reputation: 234715
If a
and b
are both floating point types, then
(long)(a * 100 + 0.5) == (long)(b * 100 + 0.5)
is an adequate test for 2-decimal-place-rounding equality, subject to your not overflowing the long
type, and your being able to tolerate the limitations of binary floating point. The 0.5
constants implement the German rounding.
Upvotes: 0