As ns mentioned, C++ counter int s to twin s immediately if necessary,because no info is lost in the translation. On the other hand,going indigenous a double to one int needs rounding off. C++ no performthis operation automatically, in order come make sure that you, together theprogrammer, are conscious of the lose of the fractional component of the number.

You are watching: C++ double to int

The simplest method to convert a floating-point worth to an essence is touse a typecast. Typecasting is so called due to the fact that it permits you totake a worth that belongs come one type and “cast” it into another type(in the feeling of molding or reforming, no throwing).

The syntax because that typecasting is prefer the syntax because that a duty call. Forexample:


double pi = 3.14159;int x = int (pi);
The int role returns an integer, for this reason x it s okay the worth 3. Convertingto one integer constantly rounds down, even if the fraction part is0.99999999.

For every type in C++, there is a corresponding role that typecastsits dispute to the suitable type.


Q-1: In the lab, us measured a temperature of 7.99999999 degrees C, usingan extremely precise measuring device. Now we are writing a programto do some calculations with our data. Think about the adhering to C++code.


int main () double temp = 7.99999999; int roundedTemp = int (temp); cout roundedTemp;
What is the worth of roundedTemp?

tempThis is the surname of a variable. Just the value of a change will print with cout. 8Remember that converting to an integer constantly rounds down. 7Correct! 8.0This is no an creature data type, and also it"s no the ideal number. 7.0This is not an creature data type.

Q-2: Your last grade is composed of your mean performance on test 1 and also exam 2.Your professor is making use of C++ to grade the exams and enables you to choose whichmethod you’d like your exam to it is in graded.


double exam1 = 88.8;double exam2 = 72.7;double exam2 = 97.9;
Method 1:


double last = (int(exam1) + int(exam2) + int(exam3)) / 3;
Method 2:


int last = int((exam1 + exam2 + exam3) / 3);
Which an approach would you choose and also why?

Method 1: final is a double, definition my last grade willhave an ext digits previous the decimal, and also will be higher than the intin method 2.

Although last is a double, it doesn’t have any type of digits pastthe decimal because of the integer division.

Method 1: the rounding happens in ~ the beginning, therefore all three of mytest scores will be rounded come the nearest int, i beg your pardon in mine case, willround all of them up.

Converting come an int constantly rounds down, also if your double is veryclose to the following integer.

Method 2: last is one int, therefore it it s okay rounded up.

See more: Game Of Thrones Season 6 Episode 7 Download, Watch Game Of Thrones Season 6 Episode 7 Online

Converting come an int always rounds down, even if your dual is veryclose to the following integer.

Method 2: the rounding happens at the an extremely end, so mine grade will be higher!

Always conserve your rounding till the end!


You have attempted that activities on this web page

| ago to peak