Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

floating point value

This topic is 1834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am able to input a number like 3.15 into a variable. I want to extract the 3 from the value and use it later. Let me know if you need more info.

Share this post


Link to post
Share on other sites
Advertisement

In addition to the obvious answer of casting it to an int, you could try passing it to std::floor() in <cmath>, if it's just going to be converted back to a floating point number anyway. Beware, though, that casting to an int always truncates toward zero, while calling std::floor() with a negative parameter will round toward negative infinity. If your floating point number is outside of the range representable by an integer, you must use std::floor().

Share this post


Link to post
Share on other sites

Since this is C++, there's a handy little C function for this... goes by the name of modf. 

Share this post


Link to post
Share on other sites

well this code works except that pence outputs 0.2 and I want it to output 2

 
 
float pounds=0, shillings=0, pence = 0;
 
 
float decimal_pounds;
cout << 
 
"Enter decimal pounds: ";
cin >> decimal_pounds;
cout << endl;
pounds=(
 
int)decimal_pounds;
decimal_pounds= decimal_pounds - pounds;
shillings = (
 
int) (decimal_pounds * 20.0);
pence = (
 
float)(decimal_pounds * 20.0)- shillings ;
cout << 
 
"Equivalent in old notation = " << '\x9c' << pounds << "." << shillings << "." << pence << endl;
system (
 
"pause");
 
 
return 0;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!