Jump to content
• Advertisement

Public Group

# floating point value

This topic is 2167 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

##### Share on other sites
Advertisement

cast it to an int.

#### Share this post

##### Share on other sites

cool I will try that

#### Share this post

##### Share on other sites

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

##### Share on other sites

is there anyway to extract the .51 from a variable.

#### Share this post

##### Share on other sites

Subtract the integer part from the value.

#### Share this post

##### Share on other sites

well thanks for all the help

#### Share this post

##### Share on other sites

Or just try:

3.15 mod 1 = 0.15

3.15 - 0.15 = 3

#### Share this 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

##### 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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Now

• 21
• 11
• 9
• 17
• 13
• Advertisement
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!