Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ā¤ šŸ’»
We are community that shares solutions with each other.

If you think you have something to add here.

you are welcome with your fresh ideas - List Articles

Java - convert int to double

0 contributions
6 points

Short solution:

int intNumber = 200;
double doubleNumber = (double) intNumber; // 200

Another simple solutions:

// solution 2 (implicit casting)
int intNumber2 = 200;
double doubleNumber2 = intNumber2; // 200

// solution 3
int intNumber3 = 200;
double doubleNumber3 = 1.0d * intNumber3; // 200 // or: 1.0 * intNumber3

// solution 4
int intNumber4 = 200;
Double doubleNumber4 = new Double(intNumber4); // 200

// solution 5
int intNumber5 = 200;
Double doubleNumber5 = Double.valueOf(intNumber5); // 200

// solution 6
Integer number6 = 200;
double doubleNumber6 = number6.doubleValue(); // 200

In java we canĀ convert int to double in following ways.

1. Using explicit double casting

public class Example1 {

    public static void main(String[] args) {

        int intNumber = 200;
        double doubleNumber = (double) intNumber;
        doubleNumber += 0.5;

        System.out.println(doubleNumber); //  200.5
    }
}

Output:

200.5

2. Using implicit double casting

public class Example2 {

    public static void main(String[] args) {

        int intNumber = 200;
        double doubleNumber = intNumber;
        doubleNumber += 0.5;

        System.out.println(doubleNumber); //  200.5
    }
}

Output:

200.5

3. Using multiplication by double number

public class Example3 {

    public static void main(String[] args) {

        int intNumber = 200;
        double doubleNumber = 1.0d * intNumber; // or: 1.0 * intNumber
        doubleNumber += 0.5;

        System.out.println(doubleNumber); //  200.5
    }
}

Output:

200.5

4. ConvertĀ int to Double using Double constructor

public class Example4 {

    public static void main(String[] args) {

        int intNumber = 200;
        Double doubleNumber = new Double(intNumber);
        doubleNumber += 0.5;

        System.out.println(doubleNumber); //  200.5
    }
}

Output:

200.5

5. Convert int to Double using Double.valueOf()

public class Example5 {

    public static void main(String[] args) {

        int intNumber = 200;
        Double doubleNumber = Double.valueOf(intNumber);
        doubleNumber += 0.5;

        System.out.println(doubleNumber); //  200.5
    }
}

Output:

200.5

6. Convert Integer to Double using Integer doubleValue()

public class Example6 {

    public static void main(String[] args) {

        Integer number = 200;
        double doubleNumber = number.doubleValue();
        doubleNumber += 0.5;

        System.out.println(doubleNumber); //  200.5
    }
}

Output:

200.5

Ā 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey šŸ‘‹
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more