Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Set precision based on a condition in cout

Set precision based on a condition in cout

Scheduled Pinned Locked Moved C / C++ / MFC
testingbeta-testingquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amine zawix
    wrote on last edited by
    #1

    Hello

    #include
    #include
    #include
    #include
    using namespace std;

    	double asDouble ( double price\_numerator, double number\_of\_decimal )
    	{
    
    		//if (price\_numerator == -2147483648)
    	//	{
    
    	//		return -214748.3648;
    		//}
    		return price\_numerator /  pow10(number\_of\_decimal);
    	}
    

    int main()
    {
    bool someBool = true; //just for testing
    double x = asDouble(-2147483648 , 4) ;
    double y = asDouble(5456 , 4) ;
    cout<< (someBool ? std::setprecision(10) : std::setprecision(6)) << x<< endl;
    cout<< (someBool ? std::setprecision(10) : std::setprecision(6) )<< y<< endl;

    return 0;
    

    }

    so I want the precision to be set based on condition, but the output is : -214748 0.5456 so I want this output : -214748.3648 0.5456

    1 1 Reply Last reply
    0
    • A amine zawix

      Hello

      #include
      #include
      #include
      #include
      using namespace std;

      	double asDouble ( double price\_numerator, double number\_of\_decimal )
      	{
      
      		//if (price\_numerator == -2147483648)
      	//	{
      
      	//		return -214748.3648;
      		//}
      		return price\_numerator /  pow10(number\_of\_decimal);
      	}
      

      int main()
      {
      bool someBool = true; //just for testing
      double x = asDouble(-2147483648 , 4) ;
      double y = asDouble(5456 , 4) ;
      cout<< (someBool ? std::setprecision(10) : std::setprecision(6)) << x<< endl;
      cout<< (someBool ? std::setprecision(10) : std::setprecision(6) )<< y<< endl;

      return 0;
      

      }

      so I want the precision to be set based on condition, but the output is : -214748 0.5456 so I want this output : -214748.3648 0.5456

      1 Offline
      1 Offline
      11917640 Member
      wrote on last edited by
      #2

      I made the following change, and result is as expected:

      double x = asDouble(-2147483648.0, 4);    // instead of asDouble(-2147483648, 4);
      

      Your code is not compiled for me, the error is:

      error C4146: unary minus operator applied to unsigned type, result still unsigned

      So, your conditional precision is OK, the problem is that 2147483648 is more than maximal unsigned int value, and applying unary minus operator to it gives compiler error in my case, and unexpected result in your case. Probably your compiler gives some warning for this line, don't ignore it. Output:

      -214748.3648
      0.5456

      Tested with VC++ 2015.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups