- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to convert Double to numeric primitive data types
Let’s say we have Double object here.
Double obj = new Double("39.45");
Now, if we want to convert this Double to short primitive data type. For that, use the in-built shortValue() method.
// converting to short primitive types short shortObj = obj.shortValue(); System.out.println(shortObj);
In the same way convert Double to another numeric primitive data type int. For that, use the in-built intValue() method.
// converting to int primitive types int intObj = obj.intValue(); System.out.println(intObj);
The following is an example wherein we convert Double to numeric primitive types short, int, float, etc.
Example
public class Demo { public static void main(String args[]) { Double obj = new Double("39.45"); int intVal = obj.intValue(); System.out.println(intVal); byte byteVal = obj.byteValue(); System.out.println(byteVal); short shortVal = obj.shortValue(); System.out.println(shortVal); float floatVal = obj.floatValue(); System.out.println(floatVal); double doubleVal = obj.doubleValue(); System.out.println(doubleVal); } }
Output
39 39 39 39.45 39.45
Advertisements