从String中删除尾随零?

时间:2014-09-17 10:01:50

标签: java

我有一个java web服务,它返回一个字符串类型结果列表,如下所示。

12341.0
4578231.0
25.0
4785555.0
347895666.0

现在我如何删除试验零?我想要的是字符串列表:

12341
4578231
25
4785555
347895666

我该怎么做?

7 个答案:

答案 0 :(得分:1)

class Test
 {

     public static void main(String[] args) throws ParseException {
         Number parse = NumberFormat.getNumberInstance().parse("12341.0");
         System.out.print(parse);
     }

  }

答案 1 :(得分:1)

使用正则表达式:

str = str.replaceAll("\\.0+$", "");

答案 2 :(得分:1)

试试这个,这是我用的正则表达式。

result = result.indexOf(".") < 0 ? result : result.replaceAll("0*$", "").replaceAll("\\.$", "");

或使用十进制格式

String result = "347895666.0";
DecimalFormat decimalFormat = new DecimalFormat("###.#");
String result = decimalFormat.format(Double.valueOf(s));
System.out.println(result);

答案 3 :(得分:0)

这可能会有所帮助:

 string.replaceFirst("[.]0*$","")

答案 4 :(得分:0)

您可以在Java中使用substring (Java 7)substring (Java 8)方法。

String result = "12341.0";
String stripped = result.substring(0, result.indexOf("."));

答案 5 :(得分:0)

使用小数格式......

Double num= Double.parseDouble("1.300");
DecimalFormat format = new DecimalFormat("0.#");
System.out.println(format.format(num));

输出:1.3

答案 6 :(得分:0)

如果您从网络服务获得双倍,可以使用此

System.out.println((int)Double.parseDouble("12341.0"));
相关问题