怎么去掉多余的0

如果输入了0.00,怎么去掉多余的0,只在文本框中显示 0.0,如果用户输入-0.0,也要转换成0.0

Double.paseDouble("0.00").toString()

伪代码:
if value == 0.00 or value == -0.00 //也可以把数字用引号引起来
value = 0.0

package test;

/**
 * 去掉多余的.与0
 * @author Hust
 * @Time 2011-11-7
 */
public class TestString {

public static void main(String[] args) {
Float f = 1f;
System.out.println(f.toString());......
答案就在这里:去掉多余的.与0
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

new DecimalFormat("0.0").format(num)); //无论num为任何值都只显示0.0

new DecimalFormat("#.#").format(num));//num小数点前后如果有值就显示,没有则不显示

ps:DecimalFormat()中,
0 就是阿拉伯数字0;
#为非0值;
.是小数点

具体可以查一下文档

package test;

/**

  • 去掉多余的.与0
  • @author Hust
  • @Time 2011-11-7 */ public class TestString {

public static void main(String[] args) {
Float f = 1f;
System.out.println(f.toString());//1.0
System.out.println(subZeroAndDot("1"));; // 转换后为1
System.out.println(subZeroAndDot("10"));; // 转换后为10
System.out.println(subZeroAndDot("1.0"));; // 转换后为1
System.out.println(subZeroAndDot("1.010"));; // 转换后为1.01
System.out.println(subZeroAndDot("1.01"));; // 转换后为1.01
}

/**

  • 使用java正则表达式去掉多余的.与0
  • @param s
  • @return */ public static String subZeroAndDot(String s){ if(s.indexOf(".") > 0){ s = s.replaceAll("0+?$", "");//去掉多余的0 s = s.replaceAll("[.]$", "");//如最后一位是.则去掉 } return s; }