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

Discussion in 'C++' started by da Vinci, Jul 24, 2004.

1. ### da VinciGuest

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.

da Vinci, Jul 24, 2004

2. ### Mike WahlerGuest

Re: Double convert to Integer and Double....

"da Vinci" <> wrote in message
news:...
> 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

Mike Wahler, Jul 24, 2004

3. ### John HarrisonGuest

On Sat, 24 Jul 2004 03:41:19 GMT, 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?
>
> 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

John Harrison, Jul 24, 2004
4. ### marbacGuest

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

marbac, Jul 24, 2004
5. ### marbacGuest

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

marbac, Jul 24, 2004
6. ### da VinciGuest

On Sat, 24 Jul 2004 03:41:19 GMT, 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?
>
>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.

da Vinci, Jul 31, 2004