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. 將字符串轉換爲雙精度?
- 4. 將字符串轉換爲雙精度 - 丟失精度
- 5. 轉換精度雙爲字符串
- 6. 將整數,雙精度型轉換爲Java中的字符串
- 7. VB.NET - 將字符串轉換爲雙精度字符並返回
- 8. 將字符串轉換爲雙精度值
- 9. Spring:將可空字符串值轉換爲雙精度值
- 10. 爲什麼我無法將字符串轉換爲雙精度?
- 11. 無法將雙精度值作爲字符串轉換爲textview
- 12. 如何將雙精度值轉換爲字符串值
- 13. 如何將6020494385.89982字符串值轉換爲雙精度值
- 14. 將字符串轉換爲int和雙精度型
- 15. 將字符串轉換爲PHP中的雙精度值
- 16. 將VBA字符串轉換爲雙精度值
- 17. 將字符串轉換爲雙精度於android
- 18. 無法將字符串轉換爲雙精度vb2008?
- 19. PromptAndInput,無法將字符串轉換爲雙精度(C#)
- 20. 將UITextField字符串轉換爲UITextField中的雙精度操作
- 21. 將字符串轉換爲雙精度錯誤
- 22. 將字符串轉換爲雙精度值
- 23. 將字符串轉換爲雙精度丟失
- 24. 如何將JTextField字符串轉換爲雙精度?
- 25. 將雙精度轉換爲字符串(C#)
- 26. 在Swift中將字符串轉換爲雙精度(Xcode v 7.0.1)
- 27. 如何將百分比字符串轉換爲雙精度?
- 28. 將字符串百分比轉換爲雙精度值
- 29. 將字符串轉換爲已知格式的雙精度值
- 30. 將字符串轉換爲雙精度的C++錯誤
確定`NumberFormat.getInstance`總是返回DecimalFormat`的`實例。並且不用手動替換逗號來打敗本地化的整個想法? – 2010-03-10 09:19:54
@Tadeusz:問題是關於「帶逗號的數字」 - 而不是「當前語言環境中的數字」。如果問題是特定的,那麼答案是特定的。至於NumberFormat.getInstance可能不會返回DecimalFormat - 是的,這是一種可能性。我不確定最好的方式,說實話;這個API並不是特別有用:( – 2010-03-10 09:22:33