Apcsa Casting And Converting

If you are not familiar with the folk tale The Little Glass Slip , a magical entity does magic that changes from a movie costume to a formal costume. Numerical-Based Casting

When we use casting to change the data type of numeric values, it affects the value of the data.

To cast, we need to use the Casting Operator to notify a setter that we want to create a temporary value that has changed The type of data. Narrow Type Casting

double to Example:

double numDouble = 12.99; System.out.println("Double value: " + numDouble); // convert a double to int int numInt = (int) doubleNum; System.out.println("Int value: " + numInt);

The result will be:

Double value: 12.99 Int value: 12

When we have a double to double numDouble = 12.99; System.out.println("Double value: " + numDouble); // convert a double to an int rounded to nearest value int numInt = (int) (doubleNum + 0.5); System.out.println("Int value: " + numInt);

The result will be:

Double value: 12.99 Int value: 13

Leave a Comment

Your email address will not be published. Required fields are marked *