Home > front end >  How to convert oracle code to java(how to do divide in java)
How to convert oracle code to java(how to do divide in java)

Time:09-02

I want to convert this [PL/SQL] code to Java code.

W_LEDGER_BAL := nvl(SUBSTR(W_BAL, 1, 17), 0) / 100;

I have tried to convert like this

String amountstr = _ISOResposne.get("48");
String W_LEDGER_BAL = amountstr.substring(0, 17);

where W_BAL is amountstr

I don't know how to divide it by 100. Please help me to solve this error.

CodePudding user response:

// Get the value.
String amountstr = _ISOResposne.get("48");

if (amountstr == null) {
    // If it's null, set it to zero.
    amountstr = "0";
}
else {
    // otherwise get the first 17 characters.
    // (Note that if the length is less than 17, an exception will occur.)
    amountstr = amountstr.substring(0, 17);
}
// Convert to a number.
// This will also throw an exception if 'amountstr' cannot be parsed to a number.
java.math.BigDecimal bd = new java.math.BigDecimal(amountstr);

// Divide by one hundred.
bd = bd.divide(new java.math.BigDecimal(100.0d);

// Convert the result to a string.
String W_LEDGER_BAL = bd.toString();

Refer to javadoc of class BigDecimal.

I assume you know what an exception is in Java.

I also suggest that you try to adhere to Java naming conventions.

  • Related