简体   繁体   中英

Removing trialing zeros from String?

I have a java web service which returns a list of strings type result as below.

12341.0
4578231.0
25.0
4785555.0
347895666.0

Now how can I remove trialing zeroes? what I want is list of strings as:

12341
4578231
25
4785555
347895666

how can i do it?

class Test
 {

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

  }

使用正则表达式:

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

Try this, this is what I used using regex.

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

or use Decimal Format

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

这可能会有所帮助:

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

You can use the substring (Java 7) or substring (Java 8) method in Java.

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

Use decimal format...

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

output: 1.3

如果您从Web服务中获得双倍收益,则可以使用它

System.out.println((int)Double.parseDouble("12341.0"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM