Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1
Add blogpost - convert double to int in java#20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
216 changes: 216 additions & 0 deletions_posts/java-basic/2021-03-02-convert-double-to-int.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| --- | ||
| layout: post | ||
| title: "How To Convert double To int In Java?" | ||
| author: gaurav | ||
| image: assets/images/2021-03-02/double-to-int-in-java.webp | ||
| categories: [ Java, Core Java, String] | ||
| description: "In this article you are going to learn how can we convert a double value to an integer value." | ||
| --- | ||
| In this article, we will see how we can convert a double to an int. | ||
| In java programming, you will have a double primitive value (ex 82.14), but to do the further operations you need an int value (ex. 82) so let's see how to convert double to int in java. | ||
| There are three ways you can convert double to int. I will list them all below and, then we will see them one by one. | ||
| 1. convert double to int - using typecasting | ||
| 2. convert double to int - using `Math.round()` | ||
| 3. convert double to int - using `Double.IntValue()` | ||
| > You may like to visit: | ||
| > [How To Convert An Integer To String In Java](https://coderolls.com/convert-int-to-string/) | ||
| ## 1. convert double to int - using typecasting | ||
| We know `double` is a 64-bit primitive value, and int is a 32-bit primitive value. So, to convert double to int, we can downcast the double value to int. | ||
| I have given a simple example below that shows to convert double to int using typecasting. | ||
| ```java | ||
| /** | ||
| * A java program to convert double to int using typecasting | ||
| * @author Gaurav Kukade at coderolls.com | ||
| **/ | ||
| public class DoubleToIntUsingTypecasting{ | ||
| public static void main(String []args){ | ||
| double doubleValue = 82.14; // 82.14 | ||
| System.out.println("doubleValue: "+doubleValue); | ||
| //typecase double to int | ||
| int intValue = (int) doubleValue; // 82 | ||
| System.out.println("intValue: "+intValue); | ||
| } | ||
| } | ||
| ``` | ||
| Output: | ||
| ```java | ||
| doubleValue: 82.14 | ||
| intValue: 82 | ||
| ``` | ||
| The problem with the typecasting is that it will truncate the value after the decimal point. It will not round it. | ||
| In the case of 82.14, we will get an int value of 82, which looks ok. But when we have a double value like 82.99, we will get only 82 and loss the 0.99 that is ~1. | ||
| It may create an issue in your calculations. | ||
| In the case of 82.99, it should be rounded to 83 and then converted to int. | ||
| It is not possible with typecasting, but our next solution can achieve it. | ||
| ## 2. convert double to int - using `Math.round()` | ||
| `Math.round()` method will round the floating-point value to the nearest long value. Then we can typecast it to the int. | ||
| I have given a simple java program below that shows how to convert double to int using the `Math.round()` method. | ||
| ```java | ||
| /** | ||
| * A java program to convert double to int using | ||
| * Math.round() method | ||
| * @author Gaurav Kukade at coderolls.com | ||
| **/ | ||
| public class DoubleToIntUsingRoundMethod{ | ||
| public static void main(String []args){ | ||
| // case 1 | ||
| double doubleValue = 82.14; // 82.14 | ||
| System.out.println("doubleValue: "+doubleValue); | ||
| //typecase double to int | ||
| int intValue = (int) Math.round(doubleValue); // 82 | ||
| System.out.println("intValue: "+intValue); | ||
| System.out.println(); | ||
| // case 2 | ||
| double nextDoubleValue = 82.99; // | ||
| System.out.println("nextDoubleValue: "+nextDoubleValue); | ||
| // Math.round(nextDoubleValue) returns long value | ||
| //typecase long to int | ||
| int nextIntValue = (int) Math.round(nextDoubleValue); // 83 | ||
| System.out.println("nextIntValue: "+nextIntValue); | ||
| } | ||
| } | ||
| ``` | ||
| Output: | ||
| ```java | ||
| doubleValue: 82.14 | ||
| intValue: 82 | ||
| nextDoubleValue: 82.99 | ||
| nextIntValue: 83 | ||
| ``` | ||
| ## 3. convert double to int - using `Double.intValue()` | ||
| In this way, we will convert the double primitive value to the `Double` wrapper class, and then we can use the `intValue()` method of the `Double` wrapper class. | ||
| This method does not round the value before converting it to the int value. It will remove the digits after the decimal point. | ||
| I have given a simple java program below that shows how to convert double to int using the `Double.IntValue()` method. | ||
| ```java | ||
| /** | ||
| * | ||
| * A java program to convert double to int using | ||
| * Double.intValue() method | ||
| * @author Gaurav Kukade at coderolls.com | ||
| * | ||
| **/ | ||
| public class DoubleToIntUsingIntValueMethod{ | ||
| public static void main(String []args){ | ||
| double doubleValue = 82.14; // 82.14 | ||
| System.out.println("doubleValue: "+doubleValue); | ||
| //create Double wrapper object | ||
| Double doubleValueObject = new Double(doubleValue); | ||
| //typecase double to int | ||
| int intValue = doubleValueObject.intValue(); // 82 | ||
| System.out.println("intValue: "+intValue); | ||
| } | ||
| } | ||
| ``` | ||
| Output: | ||
| ```java | ||
| doubleValue: 82.14 | ||
| intValue: 82 | ||
| ``` | ||
| ## Conclusion | ||
| We can convert double to int in java using the three ways given below. | ||
| **1. convert double to int - using typecasting** | ||
| In this method we typecast the double value to int as give below, | ||
| ```java | ||
| int intValue = (int) Math.round(doubleValue); | ||
| ``` | ||
| But in this way, we will lose the value after the decimal point. It will not do the rounding before converting double to int. | ||
| **2. convert double to int - using `Math.round()`** | ||
| In this way, we use the `Math.round()` method for the rounding purpose. | ||
| `Math.round()` method round the double value to the nearest long, and then we can typecast long to the int as given below. | ||
| ```java | ||
| int nextIntValue = (int) Math.round(nextDoubleValue); | ||
| ``` | ||
| **3. convert double to int - using `Double.IntValue()`** | ||
| In this way, we convert the `double` value to the `Double` wrapper class, and then we use the `Double.intValue()` method to get the int value. | ||
| ```java | ||
| //create Double wrapper object | ||
| Double doubleValueObject = new Double(doubleValue); | ||
| //typecase double to int | ||
| int intValue = doubleValueObject.intValue(); | ||
| ``` | ||
| In this way also we will lose the digits after the decimal points. | ||
| So this is how we do convert a double to int in java. You can check all three ([DoubleToIntUsingTypecasting.java](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-basic/DoubleToIntUsingTypecasting.java), [DoubleToIntUsingRoundMethod.java](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-basic/DoubleToIntUsingRoundMethod.java), [DoubleToIntUsingIntValueMethod.java](https://github.com/coderolls/blogpost-coding-examples/blob/main/java-basic/DoubleToIntUsingIntValueMethod.java)) programs on GitHub | ||
| You can read more about [string to int](https://coderolls.com/convert-int-to-string/) and [int to string](https://coderolls.com/convert-string-to-int/) conversion. | ||
| ------- | ||
| You can visit my [YouTube channel 'coderolls'](https://www.youtube.com/channel/UCl31HHUdQbSHOQfc9L-wo3w?view_as=subscriber?sub_confirmation=1) to find more video tutorials. | ||
| If you found this article worth, support me by [giving a cup of Coffee ☕](https://www.paypal.me/GauravKukade) | ||
| ### Related Articles | ||
| - [How To Convert An Integer To String In Java](https://coderolls.com/convert-int-to-string/) | ||
| - [How to convert String to Integer in Java](https://coderolls.com/convert-string-to-int/) | ||
| - [How To Convert StringBuilder To String In Java?](https://coderolls.com/convert-stringbuilder-to-string-in-java/) | ||
| - [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/) | ||
| - [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) | ||
Binary file addedassets/images/2021-03-02/double-to-int-in-java.webp
Loading
Sorry, something went wrong.Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.