aps
aps

Reputation: 2472

Using double up to two decimal places

I want to use double up to just 2 decimal places. i.e. it will be stored upto 2 decimal places, if two double values are compared then the comparison should be based on only the first 2 decimal places. How to achieve such a thing? I mean storing, comparison, everything will be just based on the 1st two decimal places. The remaining places may be different, greater than, less than, doesn't matter.

EDIT My values arent large. say from 0 to 5000 maximum. But I have to multiply by Cos A, Sin A a lot of times, where the value of A keeps changing during the course of the program.

EDIT Look in my program a car is moving at a particular speed, say 12 m/s. Now after every few minutes, the car changes direction, as in chooses a new angle and starts moving in a straight line along that direction. Now everytime it moves, I have to find out its x and y position on the map. which will be currentX+velocity*Cos A and currentY+Velocity*Sin A. but since this happens often, there will be a lot of cumulative error over time. How to avoid that?

Upvotes: 1

Views: 10939

Answers (5)

Peter Lawrey
Peter Lawrey

Reputation: 533472

To round to two decimal places you can use round

public static double round2(double d) {
     return Math.round(d * 100) / 100.0;
}

This only does round half up.

Note: decimal values in double may not be an exact representation. When you use Double.toString(double) directly or indirectly, it does a small amount of rounding so the number will appear as intended. However if you take this number and perform an operation you may need to round the number again.

Upvotes: 1

user207421
user207421

Reputation: 310860

it will be stored up to 2 decimal places

Impossible. Floating-point numbers don't have decimal places. They have binary places after the dot.

You have two choices:

(a) don't use floating-point, as per dlev's answer, and specifically use BigDecimal;

(b) set the required precision when doing output, e.g. via DecimalFormat, an SQL column with defined decimal precision, etc.

You should also have a good look at What every computer scientist should know about floating-point.

Upvotes: 0

hoipolloi
hoipolloi

Reputation: 8044

To retain a value of 2 decimal places, use the BigDecimal class as follows:

private static final int DECIMAL_PLACES = 2;

public static void main(String... args) {
    System.out.println(twoDecimalPlaces(12.222222)); // Prints 12.22
    System.out.println(twoDecimalPlaces(12.599999)); // Prints 12.60
}

private static java.math.BigDecimal twoDecimalPlaces(final double d) {
    return new java.math.BigDecimal(d).setScale(DECIMAL_PLACES, 
        java.math.RoundingMode.HALF_UP);
}

Upvotes: 1

dlev
dlev

Reputation: 48596

Don't use a float (or double). For one, it can't represent all two-decimal-digit numbers. For another, you can get the same (but accurate) effect with an int or long. Just pretend the tens and ones column is really the tenths and hundredths column. You can always divide by 100.0 if you need to output the result to screen, but for comparisons and behind-the-scenes work, integer storage should be fine. You can even get arbitrary precision with BigInteger.

Upvotes: 2

Mitch Wheat
Mitch Wheat

Reputation: 300499

Comparing floating point values for equality should always use some form of delta/epsilon comparison:

if (Abs(value1 - value2) < 0.01 )
{
   // considered equal to 2 decimal places
}

Upvotes: 12

Related Questions