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

D

da Vinci

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.
 
M

Mike Wahler

da Vinci said:
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
 
J

John Harrison

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
 
M

marbac

da said:
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
 
D

da Vinci

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top