Interchange between BigDecimal and Long, int

In the actual development process, BigDecimal is a frequently used data type, which can be converted to and from int Long.

The conversion relationship is shown in the following code:

int to BigDecimal data type

  // int is converted to bigDecimal type 
    public  static  void intToBigDecimal(){
         int b = 5 ;
        BigDecimal a = new BigDecimal(b);
        The data type of System.out.println(a + " is "+ a.getClass().getName());
    }

Convert Lont to BigDecimal data type

  // Convert Long to bigDecimal 
    public  static  void longToBigDecimal(){
         long b = 5 ;
        BigDecimal a = new BigDecimal(b);
        The data type of System.out.println(a + " is "+ a.getClass().getName());
    }

Convert BigDecimal to Long data type

  // bigDecimal is converted to Long type 
    public  static  void bigDecimalToLong(){
        BigDecimal b = new BigDecimal(12);
        Long c = b.longValue();
        The data type of System.out.println(c +" is "+ c.getClass().getName());
    }

Convert BigDecimal to int data type

    // bigDecimal is converted to int type 
        public  static  void bigDecimalToInt(){
            BigDecimal b = new BigDecimal(12);
            int c = b.intValue();
        }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325468552&siteId=291194637