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

  • Context: Comp Sci 
  • Thread starter Thread starter wahaj
  • Start date Start date
  • Tags Tags
    C++ Integer Value
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 3K views
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?