locked
Round Float RRS feed

  • Question

  • Hi I am new to C++

    Round Numbers like 0.00929903653292374

    To become 0.001 etc, up to one after 1.0 to have three decimals.

    Tuesday, April 5, 2016 12:59 PM

Answers

  • Thank you all for your answers and good suggestion!

    Viorel_ as usual it works perfect as what vcangel suggested as well

    #include <iomanip>
    #include <sstream>
    #include <iostream>
    
    double val = 0.00929903653292374;
    	std::stringstream os;
    	os << std::fixed << std::setprecision(2) << val;
    	double newval;
    	os >> newval;

    • Marked as answer by ollp Thursday, April 7, 2016 1:42 AM
    Thursday, April 7, 2016 1:42 AM

All replies

  • Hi I am new to C++

    Round Numbers like 0.00929903653292374

    To become 0.001 etc, up to one after 1.0 to have three decimals.


    Correction

    Round Numbers like 0.00929903653292374

    To become 0.01 etc up to one after 1 to three decimals

    Tuesday, April 5, 2016 1:02 PM
  • IEEE floating point numbers cannot precisely represent most fractional, decimal numbers. For instance .5, .25, .0625, and .75 can be precisely represented but .1, .01, .05, etc. cannot. You might explore why by looking at how these numbers would be stored in their raw binary.

    You can round numbers on text output by specifying iostream manipulators but that doesn't affect the underlying storage.

    The closest you can come to rounding to hundredths in a binary floating point format is:

    x=round(100.0*x)/100.0;


    • Edited by vcangel Tuesday, April 5, 2016 1:26 PM Extra zero in .0625
    Tuesday, April 5, 2016 1:21 PM
  • The rounding can be performed during output or conversion to string. For example:

        double d = 0.00929903653292374;

        printf( "%.2f\n", d );

    or

        cout << fixed << setprecision( 2 ) << d << endl;

    • Proposed as answer by Hart Wang Wednesday, April 6, 2016 3:23 AM
    Tuesday, April 5, 2016 4:54 PM
  • Hi,

    Agree with Viorel_.

    For <tt>a</tt>, <tt>A</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt> and <tt>F</tt> specifiers: this is the number of digits to be printed after the decimal point (by default, this is 6).

    If you have free time. you can see the article.

    http://www.cplusplus.com/reference/cstdio/printf/

    Best Regards,

    Hart

    Wednesday, April 6, 2016 3:26 AM
  • The rounding can be performed during output or conversion to string. For example:

        double d = 0.00929903653292374;

        printf( "%.2f\n", d );

    or

        cout << fixed << setprecision( 2 ) << d << endl;

    Hi Viorel_ This is a console output right? If I want a function care to show me how too? In an CLR application.

    And want the numbers show in listView care to show me how? As I said I am new to this....

    Wednesday, April 6, 2016 11:51 PM
  • Thank you all for your answers and good suggestion!

    Viorel_ as usual it works perfect as what vcangel suggested as well

    #include <iomanip>
    #include <sstream>
    #include <iostream>
    
    double val = 0.00929903653292374;
    	std::stringstream os;
    	os << std::fixed << std::setprecision(2) << val;
    	double newval;
    	os >> newval;

    • Marked as answer by ollp Thursday, April 7, 2016 1:42 AM
    Thursday, April 7, 2016 1:42 AM