1

I am little bit lost with double decimal point at the moment.

I have basically two methods, which will set the values for double amount and double receive. Then another integer variable where I would like to set the (receive - amount) * 100.

For example if I have two double values and I want to set their difference to an int value, then would it be possible?

My problem is that if I try to find the difference between two values, then e.g. (10.0- 9.40), then it will be 0.599999999. How can I get 0.60 out of it inside the method and use it? I know how to use NumberFormat or DecimalFormat. Should I use one inside the method to set the number of decimal points?

  • 3
    Can you post your code? If you need precision, you can use `BigDecimal` and `BigDecimal#setScale` or `BigDecimal#round`. http://stackoverflow.com/questions/3843440/bigdecimal-setscale-and-round – riddle_me_this Apr 27 '16 at 17:43

4 Answers4

2

you can round off the value im using a decimalformat to round off the number. You can pass a double variable inside the method and this will return a number rounded off to 2 decimal points.

double RoundTo2Decimals(double val) {
    DecimalFormat df2 = new DecimalFormat("###.##");
    return Double.valueOf(df2.format(val));
}
Priyamal
  • 2,919
  • 2
  • 25
  • 52
1

You can use BigDecimal to perform the rounding, or you can use maths like this. It basically multiplies by 100, rounds and divides by 100.

/**
 * Performs a round which is accurate to within 1 ulp. i.e. for values very close to 0.5 it
 * might be rounded up or down. This is a pragmatic choice for performance reasons as it is
 * assumed you are not working on the edge of the precision of double.
 *
 * @param d value to round
 * @return rounded value
 */
public static double round2(double d) {
    final double factor = 1e2;
    return d > WHOLE_NUMBER / factor || d < -WHOLE_NUMBER / factor ? d :
            (long) (d < 0 ? d * factor - 0.5 : d * factor + 0.5) / factor;
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Easiest solution could be below. Modifications and improvements are welcomed.

       double x  =10.0;
       double y  =9.40;
       int xy =0;

       DecimalFormat df = new DecimalFormat("#.##");      

       xy = (int) (Double.valueOf(df.format(x-y))*100);
       System.out.println(xy);
Pratiyush Kumar Singh
  • 1,977
  • 3
  • 19
  • 39
0

I think I figured it out by using Math.round().

I will just ask whether my solution is a good or a bad idea to use? I am not just so familiar with BigDecimal. Long story short about the code. Example inputs are as: a = 9.40 and b = 10.0

private int difference;
private double amountDue;
private double receive;

public void setAmount(double a) {
    amountDue = a;
}

public void receive(double b) {
    receive = b;
    difference = (int)Math.round(100 * (receive - amount));

I just needed to get int difference as 0.60 * 100 = 60, but as I mentioned before then just calculating the difference caused 0.59999999.

Just an extra question. Is it ok for me to initialize int balance variable inside one method as I have done?