简体   繁体   中英

type casting int to double java?

I'm practicing debugging and there is a question that i'm unsure on, it's asking what is wrong and what can I do to fix it I read the book chapter on it but not specific enough. pasted from the book:

int a = 26.4 ^ When you compile, this is the message:

Test.Java:8: possible loss of precision int a = 26.4;

required: int found : double 1 error

I have a decent understanding on why there is an error, because of how double has a higher precedence than int and how an int cant necessarily store a double value.

My question is, is there a way to type cast variable a into a double type? Or is the only way to fix this by changing a from int to double?

thanks

The only possibilities you have are:

  • Type cast the double into an int if you want to store your number as an integer:

     int a = (int)26.4 // so a will be 26 

    (you will obviously lose precision this way)

  • Store the number as a double to keep the precision:

     double a = 26.4 

Casting will not help at anything, look at the code below:

//int a = 26.4; // gives compile error
int a = (int) 26.4; // gives 26
double b = a; // gives 26.0
double c = (double) a; // also gives 26.0

I have a decent understanding on why there is an error, because of how double has a higher precedence than int and how an int cant necessarily store a double value.

Although "precedence" isn't the correct terminology, you seem to understand at the problem at some basic level. When you cast a double to an int , you can lose information. In this case, 26.4 will be truncated to 26 .

My question is, is there a way to type cast variable a into a double type?

A typecast changes the type of a value from one type to another. You cannot use typecasting to change the type of a variable . Once a is declared as an int , it is always an int . You cannot change that. However, the value stored in a can be cast to other types when it is used in expressions.

There are a few ways to fix this, and which is right depends on the application.

The first to to keep the left-hand type as int , and cast the right-hand side to int :

int a = (int) 26.4;

This is right if rounding toward zero, and limiting values to the range Integer.MIN_VALUE and Integer.MAX_VALUE is okay for your application.

Next, you might change the type of the left-hand side to double .

double a = 26.4;

This would be right if further operations using a can be performed with double . This won't work if you have to have an int value, for example, in order to pass to some API that requires int , or to specify the length or index of an array.

Or, you might need to explicitly control the rounding and range checking using the Math API. For example, to round to the nearest integer, and throw an exception if the value is too large for an int :

int a = Math.toIntExact(Math.round(26.4));

I think the follow code may help you. In the below code, it is converting a double into an integer variable, although it is a little tedious process for such a simple thing.

double a=26.4; int b =(int)Math.round(a);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM