Comp Sci Changing a double value to an integer value in c++

AI Thread Summary
To convert a double value to an integer in C++, the static_cast<int>(dblVal) method is suggested, where dblVal is the double variable. In this case, if speed = 9.23, the integer part can be stored in speedM simply by using speedM = speed, which allows for implicit casting. The discussion emphasizes the importance of understanding variable types and casting in C++. Additionally, it encourages using a C++ compiler to test and experiment with code. This approach simplifies the conversion process for new programmers.
wahaj
Messages
154
Reaction score
2

Homework Statement


I'm making a program which will convert mph to minutes and seconds per mile. For example my program will take 6.5 mph and convert it to 9 minutes and 13.8 seconds per mile. But being new to programming I don't know how to convert a double value to an int value (I believe its called casting). So the book gives me the following formula/ equation
intValue = static_cast<int>(dblVal);

if I have the variable speed = 9.23 (thats 6.5 mph converted to miles per min) and I want to store the integer part in another variable called speedM , where in the above equation do I put speed and speedM?

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
wahaj said:

Homework Statement


I'm making a program which will convert mph to minutes and seconds per mile. For example my program will take 6.5 mph and convert it to 9 minutes and 13.8 seconds per mile. But being new to programming I don't know how to convert a double value to an int value (I believe its called casting). So the book gives me the following formula/ equation
intValue = static_cast<int>(dblVal);

if I have the variable speed = 9.23 (thats 6.5 mph converted to miles per min) and I want to store the integer part in another variable called speedM , where in the above equation do I put speed and speedM?

Homework Equations





The Attempt at a Solution


Look at the variable names! dblVal will be your double. intValue will be your int. And in a case like this you don't even need to explicitly cast them. speedM=speed will work fine. It will implicitly cast. Don't you have a C++ compiler you can use to try these things out?
 

Similar threads

Replies
5
Views
2K
Replies
3
Views
1K
Replies
6
Views
3K
Replies
5
Views
2K
Replies
7
Views
3K
Replies
5
Views
2K
Replies
2
Views
1K
Back
Top