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) ; } }