Public Group

floating point value

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

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 on other sites

cast it to an int.

Share on other sites

cool I will try that

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 on other sites

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

Share on other sites

Subtract the integer part from the value.

Share on other sites

well thanks for all the help

Share on other sites

Or just try:

3.15 mod 1 = 0.15

3.15 - 0.15 = 3

Share on other sites

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

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;


1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• Forum Statistics

• Total Topics
632948
• Total Posts
3009398
• Who's Online (See full list)

There are no registered users currently online

×