Floating Point

Discussion in 'C++' started by Bob Smith, Dec 26, 2003.

  1. Bob Smith

    Bob Smith Guest

    "" wrote:

    > Hi,
    >
    > A pice of code.
    >
    > float A = 10;
    > float B = 7;
    > float result;
    >
    > result = A / B;
    >
    > when i run this program, i've got as result 1,4285714.... etcetc.
    > is it possible to get only two or one decimals after the floating point? And how?
    >
    > Thanks in advance.
    > Geert


    round it
    /B
     
    Bob Smith, Dec 26, 2003
    #1
    1. Advertising

  2. Bob Smith

    Guest

    Hi,

    A pice of code.

    float A = 10;
    float B = 7;
    float result;

    result = A / B;

    when i run this program, i've got as result 1,4285714.... etcetc.
    is it possible to get only two or one decimals after the floating point? And how?

    Thanks in advance.
    Geert
     
    , Dec 26, 2003
    #2
    1. Advertising

  3. Bob Smith

    Geert Joos Guest

    "Bob Smith" <> schreef in bericht
    news:...
    > "" wrote:
    >
    > > Hi,
    > >
    > > A pice of code.
    > >
    > > float A = 10;
    > > float B = 7;
    > > float result;
    > >
    > > result = A / B;
    > >
    > > when i run this program, i've got as result 1,4285714.... etcetc.
    > > is it possible to get only two or one decimals after the floating point?

    And how?
    > >
    > > Thanks in advance.
    > > Geert

    >
    > round it
    > /B

    And how do you do that, can you give me an example? I'm just a bit new in
    C++
    Thanks.
    Geert
    >
     
    Geert Joos, Dec 26, 2003
    #3
  4. Bob Smith

    Adam Fineman Guest

    Geert Joos wrote:
    > "Bob Smith" <> schreef in bericht
    >>
    >>round it
    >>/B

    >
    > And how do you do that, can you give me an example? I'm just a bit new in
    > C++


    Here's one way:

    #include <iostream>
    #include <cmath>

    using namespace std;

    int
    main()
    {
    float A = 10;
    float B = 7;

    float result = A / B;

    float prec = 2.0;
    float shift_factor = pow(10, prec);

    cout << round(result * shift_factor) / shift_factor << '\n';

    return 0;
    }

    Note that the above method is not particularly robust, elagent, or
    efficient. It's probably not even accurate for arbitrary values of
    'result'. Use at your own risk.

    - Adam

    --
    Reverse domain name to reply.
     
    Adam Fineman, Dec 26, 2003
    #4
  5. Bob Smith

    Alexandros Guest

    escribió:
    > Hi,
    >
    > A pice of code.
    >
    > float A = 10;
    > float B = 7;
    > float result;
    >
    > result = A / B;
    >
    > when i run this program, i've got as result 1,4285714.... etcetc.
    > is it possible to get only two or one decimals after the floating point? And how?
    >
    > Thanks in advance.
    > Geert


    If what you want is to display the number with 2 decimals although it
    actually has more you can do the following:


    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main()
    {
    float a=3.239487;
    float b=23.234;
    float r=b/a;

    cout << setprecision(3) << r << endl;

    return 0;
    }
     
    Alexandros, Dec 26, 2003
    #5
  6. Bob Smith

    Jeff Schwab Guest

    wrote:
    > Hi,
    >
    > A pice of code.
    >
    > float A = 10;
    > float B = 7;
    > float result;
    >
    > result = A / B;
    >
    > when i run this program, i've got as result 1,4285714.... etcetc.
    > is it possible to get only two or one decimals after the floating point? And how?
    >
    > Thanks in advance.
    > Geert




    #include <iostream>

    /* Truncate a number to (at most) two digits past the decimal point.
    * NB: This implementation is naive.
    */
    template< typename T >
    T round2( T const& t )
    {
    return int( t * 100 ) / 100.0;
    }

    int main( )
    {
    float A = 10;
    float B = 7;
    float result;

    result = A / B;

    std::cout << result << '\n';
    std::cout << round2( result ) << '\n';
    }
     
    Jeff Schwab, Dec 26, 2003
    #6
  7. Bob Smith

    Walter Guest

    "" <> wrote in message
    news:...
    > float A = 10;
    > float B = 7;
    > float result;
    >
    > result = A / B;
    >
    > when i run this program, i've got as result 1,4285714.... etcetc.
    > is it possible to get only two or one decimals after the floating point?

    And how?

    float A = 10;
    float B = 7;
    float result;

    result = ((long)((A * 100) / B)) / 100.0;

    -Walter
    www.digitalmars.com free C/C++/D compilers
     
    Walter, Dec 27, 2003
    #7
    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. H aka N
    Replies:
    15
    Views:
    15,723
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,514
  3. Replies:
    4
    Views:
    1,307
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,373
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    274
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page