java 小数的格式化以及百分比表示

来源:互联网 发布:室内设计有什么软件 编辑:程序博客网 时间:2024/05/22 12:44

参考:http://blog.sina.com.cn/s/blog_6a0cd5e5010121tn.html


##################################################3


package first;import java.text.DecimalFormat;import java.text.NumberFormat;public class test {    private static String formattedOutputDecimal(double decimal)    {        DecimalFormat df=new DecimalFormat("######0.00");          String result=df.format(decimal);          return result;    }    private static String formattedDecimalToPercentage(double decimal)    {    //获取格式化对象    NumberFormat nt = NumberFormat.getPercentInstance();    //设置百分数精确度2即保留两位小数    nt.setMinimumFractionDigits(2);    return nt.format(decimal);    }    public static void main(String[] args){      double d=0.21343254;          String result=formattedOutputDecimal(d);    System.out.println("result = "+result);        String result2=formattedDecimalToPercentage(d);    System.out.println("result = "+result2);    }    }


0 0
原创粉丝点击