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
Click For Summary
SUMMARY

The discussion focuses on converting a double value to an integer in C++ using the static_cast method. The user seeks to convert a speed value of 9.23 (representing 6.5 mph) into an integer variable named speedM. The solution provided clarifies that the explicit casting using static_cast is unnecessary in this case, as the assignment speedM = speed will implicitly convert the double to an integer. This highlights the flexibility of type conversion in C++ when assigning values between compatible types.

PREREQUISITES
  • Understanding of C++ data types (int and double)
  • Familiarity with type casting in C++
  • Basic knowledge of variable assignment in programming
  • Experience with C++ compilers for testing code
NEXT STEPS
  • Learn about implicit vs. explicit type casting in C++
  • Explore the use of static_cast in C++ for type conversion
  • Investigate how to handle floating-point precision in C++
  • Practice writing C++ programs that involve unit conversions
USEFUL FOR

Beginner C++ programmers, students learning about type conversion, and anyone interested in implementing mathematical calculations in C++ applications.

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 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K