从 webelement 中读取字符串并将其转换为小数
Reading string from webelement and it's conversion to decimals
一种情况是我从网络元素中读取货币,它是一个字符串。需要将其转换为数字格式以进行某些损益计算。
尝试了以下代码
public class NumberFormat {
public static void main(String[] args) {
//String str = ".9967";
String str1 = "€12,32123";
String str2 = "€3.452,35";
str1 = str1.replace(",", ".").replace(".", "").replaceAll("[^0-9.]", "");
str2 = str2.replace(",", ".").replace(".", "").replaceAll("[^0-9.]", "");
double str1Ch = Double.parseDouble(str1);
double str2Ch = Double.parseDouble(str2);
System.out.println(str1Ch);
System.out.println(str2Ch);
}
}
实际结果:
1232123.0
345235.0
预期结果:
12.32123
3452.35
我没有得到我期望的结果,我需要同时执行两个转换(点到 null/empty,逗号到点)
需要知道为什么代码不起作用,以及关于读取不同国家货币并将其转换为数字格式的任何建议。
您混淆了符号逗号和句号的替换。
即首先用空字符串替换句点,然后用句点替换逗号。如下图
str1 = str1.replace(".", "").replace(",", ".").replaceAll("[^0-9.]", "");
str2 = str2.replace(".", "").replace(",", ".").replaceAll("[^0-9.]", "");
您可以使用 NumberFormat
String s1 = "€3.452,35";
Locale locale = null;
switch (s1.charAt(0)) {
case '€':
locale = Locale.FRANCE;
break;
case '$':
locale = Locale.US;
break;
//Add any other money you want
default:
//Money unexpected
}
s1 = s1.substring(1, s1.length())
.replaceAll("[. ]", "")
.replaceAll(",", ".");
System.out.println(s1);
NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
System.out.println(nf.format(new BigDecimal(s1)));
其中一种选择是创建您自己的 NumberFormat
(here based on DecimalFormat
). You can use DecimalFormatSymbols
以设置其小数点分隔符或分组分隔符。
演示:
DecimalFormat df = new DecimalFormat("€#,###.#");
DecimalFormatSymbols dfSymbols = new DecimalFormatSymbols();
dfSymbols.setDecimalSeparator(',');
dfSymbols.setGroupingSeparator('.');
df.setDecimalFormatSymbols(dfSymbols);
String str1 = "€12,32123";
String str2 = "€3.452,35";
double str1Ch = df.parse(str1).doubleValue();
double str2Ch = df.parse(str2).doubleValue();
System.out.println(str1Ch);//12.32123
System.out.println(str2Ch);//3452.35
一种情况是我从网络元素中读取货币,它是一个字符串。需要将其转换为数字格式以进行某些损益计算。
尝试了以下代码
public class NumberFormat {
public static void main(String[] args) {
//String str = ".9967";
String str1 = "€12,32123";
String str2 = "€3.452,35";
str1 = str1.replace(",", ".").replace(".", "").replaceAll("[^0-9.]", "");
str2 = str2.replace(",", ".").replace(".", "").replaceAll("[^0-9.]", "");
double str1Ch = Double.parseDouble(str1);
double str2Ch = Double.parseDouble(str2);
System.out.println(str1Ch);
System.out.println(str2Ch);
}
}
实际结果:
1232123.0
345235.0
预期结果:
12.32123
3452.35
我没有得到我期望的结果,我需要同时执行两个转换(点到 null/empty,逗号到点)
需要知道为什么代码不起作用,以及关于读取不同国家货币并将其转换为数字格式的任何建议。
您混淆了符号逗号和句号的替换。
即首先用空字符串替换句点,然后用句点替换逗号。如下图
str1 = str1.replace(".", "").replace(",", ".").replaceAll("[^0-9.]", "");
str2 = str2.replace(".", "").replace(",", ".").replaceAll("[^0-9.]", "");
您可以使用 NumberFormat
String s1 = "€3.452,35";
Locale locale = null;
switch (s1.charAt(0)) {
case '€':
locale = Locale.FRANCE;
break;
case '$':
locale = Locale.US;
break;
//Add any other money you want
default:
//Money unexpected
}
s1 = s1.substring(1, s1.length())
.replaceAll("[. ]", "")
.replaceAll(",", ".");
System.out.println(s1);
NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
System.out.println(nf.format(new BigDecimal(s1)));
其中一种选择是创建您自己的 NumberFormat
(here based on DecimalFormat
). You can use DecimalFormatSymbols
以设置其小数点分隔符或分组分隔符。
演示:
DecimalFormat df = new DecimalFormat("€#,###.#");
DecimalFormatSymbols dfSymbols = new DecimalFormatSymbols();
dfSymbols.setDecimalSeparator(',');
dfSymbols.setGroupingSeparator('.');
df.setDecimalFormatSymbols(dfSymbols);
String str1 = "€12,32123";
String str2 = "€3.452,35";
double str1Ch = df.parse(str1).doubleValue();
double str2Ch = df.parse(str2).doubleValue();
System.out.println(str1Ch);//12.32123
System.out.println(str2Ch);//3452.35