原理:

http://www.cnblogs.com/yewsky/articles/1864934.html

工具类:

http://www.cnblogs.com/szfei/archive/2012/01/30/2332151.html

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

简单方法:

JAVA中控制double类型小数点后位数的方法

有时候我们需要控制double类型小数点后的位数,下面简单介绍一下:

假如现在想把某一个double的值限制在小数点后的4位,可以这样做:

import java.text.DecimalFormat;  
  
public class Main {  
    public static void main(String[] args) {  
               DecimalFormat df = new DecimalFormat( "0.0000 ");  
               double d1 = 1.0;  
               double d2 = 4.56789;  
               System.out.println(df.format(d1));   
               System.out.println(df.format(d2));   
        }  

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

当当项目中的具体使用 

节省:¥<%
           DecimalFormat df = new DecimalFormat( "0.00 "); 
               Book book = (Book)request.getAttribute("book");
               double d1 = book.getFixedPrice()-book.getDangPrice();

   
               out.println(df.format(d1));   

    %>

posted on 2012-10-08 00:28  SwitchBlade  阅读(510)  评论(0编辑  收藏  举报