gpt4 book ai didi

java - 在 Java 中将字符串加倍

转载 作者:搜寻专家 更新时间:2023-11-01 01:38:30 26 4
gpt4 key购买 nike

我的 Java 应用程序中有两个数字。我需要将 String 转换为 Double,但数字的字符串表示形式是

, - separates decimal part of number ( example 1,5 eq 6/4 )

. - separates groups of three digits ( example 1.000.000 eq 1000000 )

.如何实现 String 到 Double 的转换?

最佳答案

这是使用 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));
}
}

输出:

"15,151.11" -> 15151.110000
"-7,21.3" -> -721.300000
"8.8" -> 8.800000

关于java - 在 Java 中将字符串加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4499149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com