我有一个字符串,其中包含格式化的货币值,例如
我想提取和处理所有货币值,但无法找出正确的正则表达式。这是我尝试过的内容:
这个的输出结果是:
45,890.00
和用逗号分隔的多个值,例如45,890.00,12,345.00,23,765.34,56,908.50
..我想提取和处理所有货币值,但无法找出正确的正则表达式。这是我尝试过的内容:
public static void main(String[] args) {
String currencyValues = "45,890.00,12,345.00,23,765.34,56,908.50";
String regEx = "\\.[0-9]{2}[,]";
String[] results = currencyValues.split(regEx);
//System.out.println(Arrays.toString(results));
for(String res : results) {
System.out.println(res);
}
}
这个的输出结果是:
45,890 //removing the decimals as the reg ex is exclusive
12,345
23,765
56,908.50
请问有人能帮我解决这个问题吗?
String.split
来分割逗号,并使用SimpleDecimalFormat
来解析值。这样你就可以控制它是否真的是一个数字,小数点分隔符格式,然后按照你想要的方式格式化该十进制值。 - heliosString.split
会很困难,因为逗号既用作字段分隔符又用作小数分隔符。 - beny23