取2位小数,如何做

解决方案 »

  1.   

    1 /**
     2  * Contains static utility methods.
     3  */
     4 public class BOUtils {
     5 
     6  public static Double getRoundedDouble( double unroundedValue ){
     7     // Get the integer portion of the value
     8     double intPortion = Math.floor( unroundedValue );
     9 
    10     // Get the unrounded decimal portion
    11     double unroundedDecimalPortion = ( unroundedValue - intPortion );
    12 
    13     /* ALERT - This next code interferes with I18N. We eventually need   */
    14     /*         to change this not assume only round to 2 decimal places. */
    15 
    16     /* Multiply the decimal places by 100, which shitfs the decimal point
    17     /* two places to the left. Then round it so that we get a round to
    18     /* two decimal places. For example, if we started with 17.655 and stripped
    19     /* off the int portion, which left .655. After we shift, we are left with
    20     /* 65.5. Then a round will gives us 66. We can then put it all back
    21     /* together */
    22     double roundedDecimalPortion = Math.round( unroundedDecimalPortion * 100 );
    23 
    24     // Shift the decimal point back two places to the right
    25     roundedDecimalPortion = roundedDecimalPortion / 100;
    26 
    27     // Add the int portion and decimal portions back together
    28     double total = intPortion + roundedDecimalPortion;
    29 
    30     return new Double( total );
    31   }
    32 }
      

  2.   

    或着是可以用:
    new DecimalFormat("0.00").format(****);
      

  3.   

    java.text.DecimalFormat df =new java.text.DecimalFormat("#.00"); 
    String aveprice=df.format(avprice);写#的是有值就写值,没值就不写
    写0的是有值就写值,没值就写0如:
    234.5678 ,截取结果就是234.57
      

  4.   

    234.5678 ,截取结果就是234.57
    尾巴收上去了
    用new DecimalFormat("0.00").format(****); 也是四舍五入了。======================================
      public static void main(String[] args) {
        double f = 23.451;
        String strf = String.valueOf(f);
        String laststr = strf.substring(strf.indexOf(".") + 1);
        if(laststr.length()>2)
          strf = strf.substring(0,strf.indexOf(".")+3);    
        System.out.println(strf);
      }
      

  5.   


    java.text.DecimalFormat df =new java.text.DecimalFormat("#.00"); 
    String aveprice=df.format(avprice);写#的是有值就写值,没值就不写
    写0的是有值就写值,没值就写0