【发布时间】:2011-12-15 23:44:03
【问题描述】:
【问题讨论】:
【问题讨论】:
为什么要处理字符串变量中的数值?
Java 是一种强类型语言,您可能会更轻松地将其转换为浮点数或双精度数,然后执行所有业务逻辑,最后格式化输出。
类似:
Double d = Double.parseDouble(s);
double val = d; //mind the auto-boxing/unboxing
//business logic
//get ready to display to the user
DecimalFormat df = new DecimalFormat("#.0000");
String s = df.format(d);
http://download.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
【讨论】:
你可以试试:
String output = "-00.8899".replace("^(-?)0*", "$1");
输出:
-.8899
【讨论】:
^([-+]?)0*