0

I have a code here that calculate a rate for payment. It works however certain value will result in numerous number of decimal points. I want the result to be converted into only 2 decimal point. How do I do this? below is the attached code:

            double rateConv=(((new Double(4.4) * transaction.getAmount())/100)+(transaction.getAmount()+new Double(0.30)));     

            System.out.println(rateConv);
            transaction.setCurrencyPsy(rateConv);
            transaction.setUserId(getLoginUserProfile().getUserId());
            transaction.setTransType(WalletConstant.TRANS_DEPOSIT);
            transaction.setIsApproved(false);
            transaction.setCreateDate(new Date());
            transaction.setIsCiTrans(false);
            transDAO.save(transaction,getLoginUserProfile(),getText("email.admin"));
            if(transaction.getDepositType().equals(WalletConstant.DEPOSIT_WIREDTRANSFER)){
                addActionMessage(getText("msg.success.tt"));
            }else{
                addActionMsg(getText("msg.success"));
            }
            transaction = new WalletTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            addActionErr(getText("Error in system.Please contact system's administrator."));
            return ERROR;
        }       
        execute();
        return "paymount";
}

Thanks in advance

Max
  • 11,136
  • 15
  • 65
  • 92
StrikeNeo
  • 57
  • 1
  • 1
  • 13
  • 3
    small advice - you should never use Double or Float for money calculations. Use BigDecimal instead. – Jakub H Feb 27 '14 at 11:37
  • If you are interested in rounding the value, have a look here http://stackoverflow.com/questions/22036885/round-a-double-in-java/22037110#22037110 – Boris Feb 27 '14 at 11:37
  • I disagree with "never use float or double in money calculations". For example, some compound interest calculations require the use of logarithms, and can be done accurately enough in double. However, BigDecimal is almost always the better choice for money. – Patricia Shanahan Feb 27 '14 at 15:30

4 Answers4

3

Use BigDecimal

BigDecimal bd = new BigDecimal(doubleValue);
bd = bd.setScale(2, RoundingMode.HALF_UP);
return bd.doubleValue();

Here RoundingMode.HALF_UP will round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

Zeeshan
  • 9,465
  • 14
  • 65
  • 93
1

There are many ways of doing what you have asked for

1.

         double d = 1.234567;            
         DecimalFormat df = new DecimalFormat("#.##");
         System.out.print(df.format(d));

2.

        double d = 1.234567;
        System.out.printf("%1$.2f", d);

but I would never ever use double for money values anyways, I recommend you to take a look at BigDecimals

EDIT: look @ Zeeshan ´s answer if you want to convert Double to Big Decimal.

but the best is to ONLY use Big Decimal for money values, then you will never have any rounding issues @ converting.

CodeFanatic
  • 11,364
  • 1
  • 16
  • 37
0

Use DecimalFormat

DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(rateConv));
Nambi
  • 11,454
  • 3
  • 34
  • 49
0

Try like this:

double amount = 123;
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);
System.out.println(df.format(amount));
Suzon
  • 739
  • 1
  • 8
  • 20