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

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

  1. da Vinci

    da Vinci Guest

    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
    #1
    1. Advertising

  2. da Vinci

    Mike Wahler Guest

    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
    #2
    1. Advertising

  3. 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
    #3
  4. da Vinci

    marbac Guest

    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
    #4
  5. da Vinci

    marbac Guest

    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
    #5
  6. da Vinci

    da Vinci Guest

    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
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,454
    Victor Bazarov
    Feb 17, 2005
  2. les ander
    Replies:
    4
    Views:
    864
    wes weston
    Oct 5, 2004
  3. Heiko Vogel
    Replies:
    3
    Views:
    548
    Method Man
    Sep 14, 2004
  4. asker123
    Replies:
    0
    Views:
    1,279
    asker123
    Jun 27, 2009
  5. Suresh V
    Replies:
    5
    Views:
    3,688
    SaticCaster
    Jul 5, 2010
Loading...

Share This Page