Example usage for java.text DecimalFormat setRoundingMode

List of usage examples for java.text DecimalFormat setRoundingMode

Introduction

In this page you can find the example usage for java.text DecimalFormat setRoundingMode.

Prototype

@Override
public void setRoundingMode(RoundingMode roundingMode) 

Source Link

Document

Sets the java.math.RoundingMode used in this DecimalFormat.

Usage

From source file:com.sdcs.courierbooking.service.UserServiceImpl.java

@Override
public String paymentUpdate(String restaurantId) {
    // TODO Auto-generated method stub

    JSONObject deliveredList = new JSONObject();

    ResultSet updatePaymentDetails = userDao.paymentUpdate(restaurantId);

    Timestamp name = null;//from w  w  w  .  jav  a 2s  .c o  m

    String name2 = "0";

    int number = 0;

    double damount = 0.00;
    double totalamount = 0.00;
    double tamount1 = 0.00;

    String address = "0";

    String lastbookingid = "";

    String rest_name = "";

    String phone = "0";
    int trackingId = 0;

    String date = "";
    if (updatePaymentDetails != null) {
        try {

            JSONArray couriersArray = new JSONArray();
            while (updatePaymentDetails.next()) {

                JSONObject couriersObject = new JSONObject();

                name = updatePaymentDetails.getTimestamp("time_of_booking");
                String amounta = updatePaymentDetails.getString("total_amount");

                address = updatePaymentDetails.getString("restaurant_address");

                phone = updatePaymentDetails.getString("restaurant_contact");

                trackingId = updatePaymentDetails.getInt("booking_id");
                lastbookingid = "" + trackingId;
                rest_name = updatePaymentDetails.getString("restaurant_name");

                damount = Double.parseDouble(amounta);
                /*   extraDistance = updatePaymentDetails.getDouble("extra_distance");
                        
                */ String DATE_FORMAT = "yyyy/MM/dd";
                SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

                date = sdf.format(name);

                if (date.equals(name2)) {

                    tamount1 = damount + tamount1;

                    number = number + 1;
                } else if (name2 != "0") {

                    couriersObject.put("time_of_booking", name2);

                    couriersObject.put("number_of_couriers", number);

                    couriersObject.put("amount", totalamount);
                    couriersObject.put("tracking_number", lastbookingid);

                    couriersArray.put(couriersObject);
                    totalamount += tamount1;
                    tamount1 = damount;

                    number = 1;

                    name2 = date;

                } else {

                    tamount1 = damount;

                    number = 1;

                    name2 = date;

                }

            }
            JSONObject couriersObject = new JSONObject();

            couriersObject.put("time_of_booking", rest_name);

            couriersObject.put("number_of_couriers", number);

            double roundOff1 = Math.round(tamount1 * 100.0) / 100.0;

            couriersObject.put("amount", roundOff1);

            couriersArray.put(couriersObject);
            totalamount += tamount1;
            String referenceNumber = "" + trackingId;
            deliveredList.put("result", true);
            deliveredList.put("all_couriers", couriersArray);
            Date date1 = new Date();
            String referenceNo = "00" + trackingId + date1.getTime();

            deliveredList.put("account_id", SdcsPaymentGatewayConstants.ACCOUNT_ID);
            deliveredList.put("address", address);
            double roundOff = Math.round(totalamount * 100.0) / 100.0;
            deliveredList.put("amount", roundOff);
            //newBookingJsonData.put("amount", "13.0");
            deliveredList.put("channel", SdcsPaymentGatewayConstants.CHANNEL);
            deliveredList.put("city", lastbookingid);
            deliveredList.put("country", SdcsPaymentGatewayConstants.COUNTRY);
            deliveredList.put("currency", SdcsPaymentGatewayConstants.CURRENCY);
            deliveredList.put("description", "Make payment for new courier");
            deliveredList.put("email", "manjunathdl@gmail.com");
            deliveredList.put("mode", SdcsPaymentGatewayConstants.MODE);
            deliveredList.put("name", rest_name);
            deliveredList.put("page_id", "2197");
            deliveredList.put("phone", phone);
            deliveredList.put("postal_code", "560072");
            deliveredList.put("reference_no", referenceNo);
            deliveredList.put("return_url", SdcsPaymentGatewayConstants.RETURN_URL);
            deliveredList.put("state", "Karnataka");

            String md5HashData = SdcsPaymentGatewayConstants.SECRET_KEY + "|"
                    + SdcsPaymentGatewayConstants.ACCOUNT_ID + "|" + address + "|" + roundOff
                    //+ "|" + "13.0" 
                    + "|" + SdcsPaymentGatewayConstants.CHANNEL + "|" + lastbookingid + "|"
                    + SdcsPaymentGatewayConstants.COUNTRY + "|" + SdcsPaymentGatewayConstants.CURRENCY + "|"
                    + "Make payment for new courier" + "|" + "manjunathdl@gmail.com" + "|"
                    + SdcsPaymentGatewayConstants.MODE + "|" + rest_name + "|" + "2197" + "|" + phone + "|"
                    + "560072" + "|" + referenceNo + "|" + SdcsPaymentGatewayConstants.RETURN_URL + "|"
                    + "Karnataka";

            String UserId = "ff";

            System.out.println("Values Before Hashing:" + md5HashData);

            SdcsPersistantValues.setPersistantValues(rest_name, "manjunathdl@gmail.com", phone, trackingId + "",
                    UserId);

            deliveredList.put("hash_values", md5HashData);

            String hasedValue;
            try {
                hasedValue = SdcsHashProvider.generateMd5Hash(md5HashData);
                deliveredList.put("secure_hash", hasedValue);
                System.out.println("Values After Hashing:" + hasedValue);
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            /*String referenceNo = "00"+ trackingId+ date1.getTime();
                    
            deliveredList.put("account_id", SdcsPaymentGatewayConstants.ACCOUNT_ID);
            deliveredList.put("address",address );
            //deliveredList.put("amount", ""+tamount);
            couriersObject.put("amount", "130.0");
            deliveredList.put("channel", SdcsPaymentGatewayConstants.CHANNEL);
            deliveredList.put("city","bangalore");
            deliveredList.put("country", SdcsPaymentGatewayConstants.COUNTRY);
            deliveredList.put("currency", SdcsPaymentGatewayConstants.CURRENCY);
            deliveredList.put("description", "Make payment for new courier");
            deliveredList.put("email", "manjunathdl@gmail.com");
            deliveredList.put("mode", SdcsPaymentGatewayConstants.MODE);
            deliveredList.put("name", rest_name);
            deliveredList.put("page_id", "2197");
            deliveredList.put("phone", phone);
            deliveredList.put("postal_code", "560072");
            deliveredList.put("reference_no", referenceNo);
            deliveredList.put("return_url", SdcsPaymentGatewayConstants.RETURN_URL);
            deliveredList.put("state", "temple");
                    
                    
            String md5HashData = SdcsPaymentGatewayConstants.SECRET_KEY
                   + "|" + SdcsPaymentGatewayConstants.ACCOUNT_ID
                   + "|" + address
                   + "|" + "130.0"
                   + "|" + SdcsPaymentGatewayConstants.CHANNEL
                   + "|" + "temple"
                   + "|" + SdcsPaymentGatewayConstants.COUNTRY
                   + "|" + SdcsPaymentGatewayConstants.CURRENCY
                   + "|" + "Make payment for new courier"
                   + "|" + "manjunathdl@gmail.com"
                   + "|" + SdcsPaymentGatewayConstants.MODE
                   + "|" + rest_name
                   + "|" + "2197"
                   + "|" + phone
                   + "|" + "560072"
                   + "|" + referenceNo
                   + "|" + SdcsPaymentGatewayConstants.RETURN_URL
                   + "|" + "temple";
                    
                    
            SdcsPersistantValues.setPersistantValues(rest_name,"sdcs.info24@gmail.com", phone,trackingId+"", "Courier");
                    
            deliveredList.put("hash_values", md5HashData);
                           
            String hasedValue;
            try {
               hasedValue = SdcsHashProvider.generateMd5Hash(md5HashData);
               deliveredList.put("secure_hash", hasedValue);
                       
            } catch (NoSuchAlgorithmException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }*/

            System.out.println(md5HashData);

            DecimalFormat df = new DecimalFormat("#.##");
            df.setRoundingMode(RoundingMode.HALF_UP);
            String tamount2 = df.format(totalamount); // 0.91239

            deliveredList.put("total_Amount", tamount2);
            deliveredList.put("refferenceId", referenceNumber);

        } catch (SQLException e) {
            deliveredList.put("result", false);

            try {
                updatePaymentDetails.close();
            } catch (SQLException sqlEx) {
                System.out.println("Exception in QuickHistoryServerImpl");

            }

        } finally {
            try {
                updatePaymentDetails.close();
            } catch (SQLException e) {
                System.out.println("Exception in QuickHistoryServerImpl");
            }
        }

    } else {
        deliveredList.put("result", false);
    }

    return deliveredList.toString();
}