hofs
hofs

Reputation: 469

Comparing double values for format in java?

I need to comparing some double values for format

For example :

double a = 18.0;
double b = 18.00;
double c = 19.0;

a == b //Comparing for format => False
a == c //Comparing for format => True

Is there any method or idea?

Upvotes: 0

Views: 180

Answers (3)

Xaren
Xaren

Reputation: 511

Maybe you can use Decimal:

java.math.BigDecimal.equals(BigDecimal val)

So

BigDecimal bg1, bg2;

    bg1 = new BigDecimal("20.0");
    bg2 = new BigDecimal("20.00");

with equals bg1 and bg2 Are not equal in value and scale

Upvotes: 2

nirjhar paul
nirjhar paul

Reputation: 21

"==" simply you wont be able to do that, use OOP, create some class and pass these two values, convert both to string, than compare, return value accordingly

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198023

There is literally no difference between a and b. double values don't know their format; they're just numbers.

If you stored them as strings, that would be different, but a and b are exactly the same values.

Upvotes: 3

Related Questions