24
A
回答
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(",", ""));
或使用DecimalFormat
與setParseBigDecimal(true)
:
DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
format.setParseBigDecimal(true);
BigDecimal number = (BigDecimal) format.parse("835,111.2");
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);
}
相關問題
- 1. 將字符串轉換爲雙精度,
- 2. 將字符串轉換爲雙精度
- 3. VB.NET - 將字符串轉換爲雙精度字符並返回
- 4. 無法將雙精度值作爲字符串轉換爲textview
- 5. 將字符串轉換爲雙精度時GSON中NumberFormatException
- 6. 將字符串轉換爲雙精度錯誤
- 7. 如何將6020494385.89982字符串值轉換爲雙精度值
- 8. 將VBA字符串轉換爲雙精度值
- 9. 如何將百分比字符串轉換爲雙精度?
- 10. 將字符串轉換爲雙精度的C++錯誤
- 11. 如何將字符串列表轉換爲雙精度?
- 12. 將字符串百分比轉換爲雙精度值
- 13. 將字符串轉換爲雙精度值
- 14. 在Swift中將字符串轉換爲雙精度(Xcode v 7.0.1)
- 15. 將字符串轉換爲雙精度字符串除外部分
- 16. 將格式化字符串轉換爲雙精度字符串,反之亦然
- 17. 如何將經度和緯度字符串轉換爲雙精度?
- 18. 從字符串「」轉換爲「雙精度型」無效
- 19. 無法將雙精度轉換爲雙精度
- 20. 將數千(和十進制)分隔符轉換爲雙精度字符串
- 21. C#:將字符串從數組轉換爲雙精度分隔符
- 22. 從java BigDecimal轉換爲雙精度丟失精度爲
- 23. 將字符串中的十進制數字轉換爲雙精度型
- 24. Java:將字符串解析爲雙精度值
- 25. 如何將單個字符轉換爲雙精度
- 26. 將字符串轉換爲雙精度,並檢查是否爲零
- 27. 將時間轉換爲雙精度
- 28. 將雙精度轉換爲QString
- 29. 如何將DBNULL轉換爲雙精度?
- 30. DirectShow - 將IGraphBuilder *轉換爲雙精度(C++)
確定`NumberFormat.getInstance`總是返回DecimalFormat`的`實例。並且不用手動替換逗號來打敗本地化的整個想法? – 2010-03-10 09:19:54
@Tadeusz:問題是關於「帶逗號的數字」 - 而不是「當前語言環境中的數字」。如果問題是特定的,那麼答案是特定的。至於NumberFormat.getInstance可能不會返回DecimalFormat - 是的,這是一種可能性。我不確定最好的方式,說實話;這個API並不是特別有用:( – 2010-03-10 09:22:33