In Java, I am trying to parse a string of format "0.##" to a float. The string should always have 2 decimal places.
public ArrayList getFixRateDetails (String merchantccy,String paymodetype,String amount)
{
String returnAmt ="";
ArrayList list= new ArrayList();
float value=-1;
try {
NiCurrencyConverterProcessor nicc= new NiCurrencyConverterProcessor();
list=nicc.getFixRateDetails(merchantccy,paymodetype);
Float i = (Float.parseFloat((String) list.get(0)));
Float j = (Float.parseFloat(amount));
value=i*j;
list.set(0, value);
list.add(2, i);
GenericExceptionLog.log("PayPalUtility.java : In getFixRateDetails() : value:"+list,"paymenttarasectionsevlet111");
DecimalFormat df = new DecimalFormat("0.00");
String ReturnAmt = df.format(returnAmt);
returnAmt=String.valueOf(value).trim();
GenericExceptionLog.log("PayPalUtility.java : In getFixRateDetails() : value:"+returnAmt,"paymenttarasectionsevlet222");
} catch (Throwable t) {
GenericAsyncLogger.Logger(t,"ERROR","DB","Transaction","MIGSTxnUtility.java","postingAmtConversion()","NA","NA","","","paymenttarasectionsevlet");
//GenericExceptionLog.exceptionJava(t,"postingAmtConversion()", "MIGSTxnUtility");
}
return list;
}
}
You are assigning the formatted output to the different variable (ReturnAmt) and in the log you are printing returnAmt. It is obvious that it will print the value without formatting.
Try
DecimalFormat formatter = new DecimalFormat("0.00");
returnAmt = formatter.format(value);
For more information refer DecimalFormat
Example:
public static void main(String...args){
String returnAmt ="";
Float i = 100f;
Float j = 200f;
Float value=i*j;
DecimalFormat formatter = new DecimalFormat("0.00");
returnAmt = formatter.format(value);
System.out.println(returnAmt);
}
OUTPUT:
20000.00
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.