Java two integer division with two decimal places

1 The first

import java.math.BigDecimal;

/**
 * @author WGR
 * @create 2020/3/17 -- 15:51
 */
public class DemoTest {

    public static void main(String[] args) {
        int a=100;
        int b=33;
        double f1 = new BigDecimal((float)a/b).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(f1);
    }
}

 

 2 second

Import the java.text.DecimalFormat; 

/ ** 
 * @author the WGR 
 * @Create 2020/3/17 - 15:51 
 * / 
public  class DemoTest { 

    public  static  void main (String [] args) {
         int A = 100 ;
         int = 33 is B ; 
        DecimalFormat DF = new new DecimalFormat ( "0.00"); // format decimal 
        String df.format NUM = (( a float ) a / B); // returns a String 
        System.out.println (num) ; 
    } 
}

 

Published 407 original articles · won praise 2 · Views 6780

Guess you like

Origin blog.csdn.net/qq_29860591/article/details/104936365