title: C++ Rounding behavior consistency for ties with sprintf
body: <p>I have a C++ program that runs on both Windows/Linux.  On Windows the program is compiled with Visual Studio 2012 and Linux it is compiled with GCC.  When converting doubles to strings using sprintf Visual Studio is using a different rounding method than the GCC compiler for ties - ie decimals ending in a 5.</p>
  
  <p>Visual Studio compiler appears to perform <a href="https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero" rel="noreferrer">round half away from zero</a> while GCC does <a href="https://en.wikipedia.org/wiki/Rounding#Round_half_to_even" rel="noreferrer">round even</a> aka bankers rounding.  </p>
  
  <p>Round even is the desired behavior.</p>
  
  <p>Is it possible to change the rounding behavior used for sprintf format strings in visual studio / windows? As I need to make the rounding behave consistently between the two.</p>
  
  <p>Here is a small sample C++ program that illustrates the above described behavior:</p>
  
  <pre><code>int main()
  {   
     char buffer[100];
  
     double x;
     for (x = -0.5; x &lt;= 10.5; x += 1.0)
     {
        sprintf(buffer,"%4g %.0f\n", x, x);
  
        std::cout &lt;&lt; buffer;
     }  
  
     return 0;
  }
  </code></pre>
  
  <p>Windows output.  Numbers are rounded away from zero:</p>
  
  <p><img src="https://i.stack.imgur.com/UHo21.png" alt="windows"></p>
  
  <p>OSX output compiled using xCode.  Numbers are rounded using round even towards the even number:</p>
  
  <p><img src="https://i.stack.imgur.com/oyQj9.png" alt="osx"></p>
  
  <p>OSX output:</p>