Java中的字符串加倍

Dam*_*mir 4 java

我在我的Java应用程序中有双数字.我需要将String转换为Double,但是数字的字符串表示是

, - 分隔数字的小数部分(例如1,5 eq 6/4)

. - 分隔三位数组(例如1.000.000 eq 1000000)

.如何将String转换为Double?

aio*_*obe 5

这是一种解决它的方法,DecimalFormat而不用担心区域设置.

import java.text.*;

public class Test {

    public static void main(String[] args) throws ParseException {

        DecimalFormatSymbols dfs = new DecimalFormatSymbols();
        dfs.setGroupingSeparator('.');
        dfs.setDecimalSeparator(',');

        DecimalFormat df = new DecimalFormat();
        df.setGroupingSize(3);

        String[] tests = { "15,151.11", "-7,21.3", "8.8" };
        for (String test : tests)
            System.out.printf("\"%s\" -> %f%n", test, df.parseObject(test));
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

"15,151.11" -> 15151.110000
"-7,21.3" -> -721.300000
"8.8" -> 8.800000
Run Code Online (Sandbox Code Playgroud)