Convert double to Int, rounded down

前端 未结 5 1538
心在旅途
心在旅途 2020-12-15 02:26

How to convert a double value to int doing the following:

Double If x = 4.97542. Convert to int x = 4.

Double If x = 4.23544. Conv         


        
5条回答
  •  情话喂你
    2020-12-15 03:22

    If the double is a Double with capital D (a boxed primitive value):

    Double d = 4.97542;
    int i = (int) d.doubleValue();
    
    // or directly:
    int i2 = d.intValue();
    

    If the double is already a primitive double, then you simply cast it:

    double d = 4.97542;
    int i = (int) d;
    

提交回复
热议问题