469,903 Members | 2,201 Online

# Help: Double convert to Integer and Double....

Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?

The reason is because of converting decimal locations into DMS. Math
is simple....

14.5624 = 14 degrees .5624 minutes. Take the .5624 * 60 = 33.744 - 33
minutes .744 seconds. Then do it all over again so .744*60 = 44.64 -
45 seconds.

Basically, I do not know how to get the whole number out into an
integer variable and taking just the decimal place numbers into a
double or float.

Any help is appreciated.
Jul 22 '05 #1
5 8079
"da Vinci" <bl***@blank.com> wrote in message
news:cf********************************@4ax.com...
Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?
The reason is because of converting decimal locations into DMS. Math
is simple....

14.5624 = 14 degrees .5624 minutes. Take the .5624 * 60 = 33.744 - 33
minutes .744 seconds. Then do it all over again so .744*60 = 44.64 -
45 seconds.

Basically, I do not know how to get the whole number out into an
integer variable and taking just the decimal place numbers into a
double or float.

Any help is appreciated.

#include <cmath>
#include <iomanip>
#include <iostream>

int main()
{
double value(14.5624);
double intpart(0);
double fracpart(modf(value, &intpart));

std::cout << std::right

<< std::setw(20) << "Value: "
<< value << '\n'

<< std::setw(20) << "Integer portion: "
<< intpart << '\n'

<< std::setw(20) << "Fractional portion: "
<< fracpart << '\n';

return 0;
}

-Mike
Jul 22 '05 #2
On Sat, 24 Jul 2004 03:41:19 GMT, da Vinci <bl***@blank.com> wrote:
Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?

The reason is because of converting decimal locations into DMS. Math
is simple....

14.5624 = 14 degrees .5624 minutes. Take the .5624 * 60 = 33.744 - 33
minutes .744 seconds. Then do it all over again so .744*60 = 44.64 -
45 seconds.

Basically, I do not know how to get the whole number out into an
integer variable and taking just the decimal place numbers into a
double or float.

Any help is appreciated.

Well you are going about it the wrong way

14.5624 * 3600 = 52424.64 second ~ 52425 seconds

Now you have an integer

52425 / 3600 = 14 degrees, remainder 2025 seconds

2025 / 60 = 33 minutes, remainder 45 seconds

Use % to get the remainder of a division.

john
Jul 22 '05 #3
da Vinci wrote:
Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?

Hi,
Integer can be received by using a cast to an integer.
The decimals are available by subtraction.
#include <iostream>
#include <cmath> // required for fabs()

using namespace std;
int main () {

double my_number=-14.5624;
int my_int = static_cast <int>(my_number); //with sign
// or: int my_int = (int)my_number;

double my_decimals = fabs(my_number-my_int); //signless

cout << "Number:" << my_number <<
"; Integer:" <<my_int<<
"; Decimals:" << my_decimals << endl;

return 0;
}

Number:-14.5624; Integer:-14; Decimals:0.5624

Regards marbac
Jul 22 '05 #4
marbac wrote:
da Vinci wrote:
Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?

Hi,
Integer can be received by using a cast to an integer.
The decimals are available by subtraction.

Works this way, but as i saw, there is a more efficient method (modf()
in cmath).

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

regards marbac
Jul 22 '05 #5
On Sat, 24 Jul 2004 03:41:19 GMT, da Vinci <bl***@blank.com> wrote:
Hi Gents,

This is what I am trying to do.

Say you have a double or a float with the value 14.5624 for example.
How could I take that variable and get the 14 into an integer variable
and the .5624 into a double or float?

The reason is because of converting decimal locations into DMS. Math
is simple....

14.5624 = 14 degrees .5624 minutes. Take the .5624 * 60 = 33.744 - 33
minutes .744 seconds. Then do it all over again so .744*60 = 44.64 -
45 seconds.

Basically, I do not know how to get the whole number out into an
integer variable and taking just the decimal place numbers into a
double or float.

Any help is appreciated.

Gents,

Thanks for the responses. Had a problem after I posted that and hadn't
been able to get on newsgroups.

I actually used the method of declaring an integer variable that was
equal to a defined double....

double dnum=25.625;
int inum = dnum

Since C++ rounds down, in every case I have tested, the integer has
always come out correctly. Then I just do a "dnum = dnum - inum" and
that gives me the integer I need and the double I need.

Appreciate the help gents.

Jul 22 '05 #6

### This discussion thread is closed

Replies have been disabled for this discussion.