1

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?

aps
  • 2,452
  • 10
  • 35
  • 47

5 Answers5

12

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
}
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
2

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.

dlev
  • 48,024
  • 5
  • 125
  • 132
  • 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. – aps Jul 02 '11 at 01:50
  • Aha, a wrinkle. In that case, why are you so keen to lop off precision? What do you (hope to) gain? – dlev Jul 02 '11 at 01:53
  • 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 velocity * Cos A and Velocity*Sin A. but since this happens often, there will be a lot of cumulative error over time. How to avoid that? – aps Jul 02 '11 at 02:09
1

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);
}
hoipolloi
  • 7,984
  • 2
  • 27
  • 28
1

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483