# finding the decimal part of a number

• 07-28-2003
Geo-Fry
finding the decimal part of a number
suppose i have a number such as 3.4, but i have no idea what the number is, aside from the fact that i know its a positive number greater than 1 with both a whole part and a decimal part. how can i figure out the decimal part without modifing the number.
• 07-28-2003
bartybasher
You could do a function with a for loop. Then as the for loop iterates. See if the i in the for loop is the same as your other number. Im diggin my self into a hole so ive mad a little example.
Code:

float getDecimal(float theDecimal)
{
for (int i = 0; i < 1000; i++)
{
if (theDecimal == (i \ 100 )
return i;
}
}

Now I have not checked this but it should work. All you have to do to get a bigger range is adjust the i < 1000 value
• 07-28-2003
alpha
if you don't know what the number is, and you want to find the part after the decimal point..

you can use a double to string function (you can use the Int to String function found in the programming FAQ and just replace the ints with doubles).

then use the find() fxn in the string class to locate the decimal.

eh, here's a quick example..

Code:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

string double_str(double num);

int main()
{
double number = 4.657;
string str_d = double_str(number);

//cout << str_d;
//used to test double_str fxn

int found = str_d.find('.');
string temp = str_d.substr(found + 1, str_d.length() - found);
// you can just use str_d.substr(found, str_d.length() - found);
// if you want to keep the decimal in the cout

cout << temp;

return 0;
}

string double_str(double num)
{
ostringstream myStream;
myStream << num << flush;

return (myStream.str());
}

I think this is what you are looking for. it's dirty and the variables could be named better, but it's just a quick example..
• 07-28-2003
double Double = 3.14152;
int whole = Double;
double decimal = Double - whole;
cout << decimal << endl;

you might get a warning assigning a double to an int, in which case you might want to cast the double to an int before assignment.
• 07-28-2003
Wledge
Code:

double modf(
double x,
double *intptr
);

Splits a floating-point value into fractional and integer parts.
#include <cmath>
• 07-28-2003
alpha
ooh, there are easier ways.. geeze, i need to get programming regularly again..
• 07-28-2003
Geo-Fry
Quote:

Code:

double modf(
double x,
double *intptr
);

can u give me an example of how i would use this? pointers always confuse me...
• 07-28-2003
Prelude
>can u give me an example of how i would use this?
Code:

#include <iostream>
#include <cmath>

int main()
{
double f = 123.456;
double integral;
double fractional;

fractional = modf(f, &integral);

std::cout<<"Fractional part: "<< fractional <<std::endl;
std::cout<<"Integral part:  "<< integral <<std::endl;
}

• 07-28-2003
major_small
why don't you do this:

Code:

...
digit=int(number);  //truncates the decimal
decimal=number%number;  //the remainder will be the decimal
...

I think that should work, and it doesn't really modify the number... this way the decimal becomes a whole number
• 07-28-2003
ygfperson
The '%' operator doesn't work with doubles and floats. Floats have a different set of commands than int's and chars. And dividing a number by itself is always 1, with no remainder.
• 07-29-2003
Geo-Fry
ok thx everyone, especially wedge and prelude, that will work. i appreciate your help
• 07-29-2003
major_small
Quote:

Originally posted by ygfperson
The '%' operator doesn't work with doubles and floats. Floats have a different set of commands than int's and chars. And dividing a number by itself is always 1, with no remainder.
oh yeah... I forgot about that... yeah, scratch that... modulous only works with int's...
• 07-29-2003
Lynux-Penguin
sigh

Code:

double blah = 3.14159265358979323; //I know most of those will be truncated....
double decimal_only = blah - (int)blah;

-LC
• 07-31-2003
SereneKaos
cmath is filled with useful math functions. *gasp* what a surprise! Anyways, here's a nice place to look them all up. As for your question, the "floor" function is what you'd want.

http://www.cplusplus.com/ref/cmath/index.html

~SK