2016-05-14 10 views
-1

In Java versuche ich eine Zeichenfolge des Formats "0. ##" zu einem Float zu analysieren. Die Zeichenfolge sollte immer 2 Dezimalstellen haben.Konvertieren Zeichenfolge 2 Dezimalstellen in Java

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; 
     } 
} 
+1

Also, was genau ist das Problem? – Mureinik

+0

Verwenden Sie 'String.format()' –

+0

Ich versuche diesen Code, aber es wird nicht konvertiert –

Antwort

0

Sie weisen die formatierte Ausgabe der anderen Variablen (ReturnAmt) zu und im Protokoll drucken Sie returnAmt. Es ist offensichtlich, dass der Wert ohne Formatierung gedruckt wird.

Versuchen

DecimalFormat formatter = new DecimalFormat("0.00"); 
returnAmt = formatter.format(value); 

Weitere Informationen finden DecimalFormat

Beispiel:

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