2009-07-08 136 views

回答

46

看看java.text.NumberFormat。例如:

import java.text.*; 
import java.util.*; 

public class Test 
{ 
    // Just for the sake of a simple test program! 
    public static void main(String[] args) throws Exception 
    { 
     NumberFormat format = NumberFormat.getInstance(Locale.US); 

     Number number = format.parse("835,111.2"); 
     System.out.println(number); // or use number.doubleValue() 
    } 
} 

取決於你使用什麼樣的數量,雖然,你可能想解析到一個BigDecimal代替。這樣做的最簡單的方法可能是:

BigDecimal value = new BigDecimal(str.replace(",", "")); 

或使用DecimalFormatsetParseBigDecimal(true)

DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US); 
format.setParseBigDecimal(true); 
BigDecimal number = (BigDecimal) format.parse("835,111.2"); 
+0

確定`NumberFormat.getInstance`總是返回DecimalFormat`的`實例。並且不用手動替換逗號來打敗本地化的整個想法? – 2010-03-10 09:19:54

+0

@Tadeusz:問題是關於「帶逗號的數字」 - 而不是「當前語言環境中的數字」。如果問題是特定的,那麼答案是特定的。至於NumberFormat.getInstance可能不會返回DecimalFormat - 是的,這是一種可能性。我不確定最好的方式,說實話;這個API並不是特別有用:( – 2010-03-10 09:22:33

5

使用java.text.DecimalFormat中:

的DecimalFormat是的具體子類 NumberFormat格式化十進制 數字。它具有多種功能 ,旨在使解析 和格式數字在任何區域設置, 包括支持西方,阿拉伯語, 和印度數字。它還支持 不同種類的數字,包括 整數(123),定點數 (123.4),科學計數法(1.23E4), 百分比(12%)和貨幣 金額(123美元)。所有這些可以是 本地化。

10

最簡單的並不總是最正確的。這是最簡單的:

String s = "835,111.2"; 
// NumberFormatException possible. 
Double d = Double.parseDouble(s.replaceAll(",","")); 

,因爲你明確說明你想更換逗號所以我假設你已經建立了自己與逗號語言環境,我沒有使用語言環境困擾是千位分隔符,時間爲小數點分隔符。如果你想要正確的(在國際化方面)行爲,這裏有更好的答案。

2

A link can say more than thousand words

// Format for CANADA locale 
Locale locale = Locale.CANADA; 
String string = NumberFormat.getNumberInstance(locale).format(-1234.56); // -1,234.56 

// Format for GERMAN locale 
locale = Locale.GERMAN; 
string = NumberFormat.getNumberInstance(locale).format(-1234.56); // -1.234,56 

// Format for the default locale 
string = NumberFormat.getNumberInstance().format(-1234.56); 


// Parse a GERMAN number 
try { 
    Number number = NumberFormat.getNumberInstance(locale.GERMAN).parse("-1.234,56"); 
    if (number instanceof Long) { 
     // Long value 
    } else { 
     // Double value 
    } 
} catch (ParseException e) { 
} 
1
There is small method to convert german price format 
    public static BigDecimal getBigDecimalDe(String preis) throws ParseException { 
     NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); 
     Number number = nf.parse(preis); 
     return new BigDecimal(number.doubleValue()); 
    } 
    ---------------------------------------------------------------------------- 
    German format BigDecimal Preis into decimal format 
    ---------------------------------------------------------------------------- 
    public static String decimalFormat(BigDecimal Preis){  
     String res = "0.00"; 
        if (Preis != null){     
         NumberFormat nf = NumberFormat.getInstance(Locale.GERMANY); 
         if (nf instanceof DecimalFormat) {      
          ((DecimalFormat) nf).applyPattern("###0.00"); 

          } 
          res = nf.format(Preis);         
        } 
      return res; 

     } 
--------------------------------------------------------------------------------------- 
/** 
* This method converts Deutsche number format into Decimal format. 
* @param Preis-String parameter. 
* @return 
*/ 
public static BigDecimal bigDecimalFormat(String Preis){   
    //MathContext mi = new MathContext(2); 
    BigDecimal bd = new BigDecimal(0.00); 
       if (!Util.isEmpty(Preis)){ 
        try { 
//      getInstance() obtains local language format 
        NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN); 
        nf.setMinimumFractionDigits(2); 
        nf.setMinimumIntegerDigits(1); 
        nf.setGroupingUsed(true); 


        java.lang.Number num = nf.parse(Preis); 
        double d = num.doubleValue(); 
         bd = new BigDecimal(d); 
        } catch (ParseException e) {       
         e.printStackTrace(); 
        }      
       }else{ 
        bd = new BigDecimal(0.00); 
       } 

       //Rounding digits 
     return bd.setScale(2, RoundingMode.HALF_UP); 

    } 
相關問題