Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could try subtracting (or adding for a negative number) a small delta that will have no effect on the rounding for numbers far enough away from the precision.</p> <p>For example, if you're rounding with <code>%.2f</code>, try this version on Windows:</p> <pre><code>printf("%.2f", 11.545 - 0.001); </code></pre> <p>Floating point numbers are notoriously problematic if you don't know what's happening under the covers. In that case, your best bet is to write (or use) a decimal type library to alleviate the problems.</p> <hr> <p>The example program:</p> <pre><code>#include &lt;stdio.h&gt; int main (void) { printf("%.20f\n", 11.545); printf("%.2f\n", 11.545); printf("%.2f\n", 11.545 + 0.001); return 0; } </code></pre> <p>outputs this in my Cygwin environment:</p> <pre><code>11.54499999999999992895 11.54 11.55 </code></pre> <p>which is okay for your specific case (it's going the wrong way but should hopefully apply in the other direction as well: you need to test it) but you should check your entire possible input range if you want to be certain this will work for all your cases.</p> <hr> <p><em>Update:</em></p> <p>Evgeny, based on your comment:</p> <blockquote> <p>It works for this specific case, but not as a general solution. For instance if the number I want to format is 0.545 instead of 11.545 then '%.2f' % (0.545 - 0.001) returns "0.54", while '%.2f' % 0.545 on Linux correctly returns "0.55".</p> </blockquote> <p>that's why I said you would have to check the entire range to see if it would work, and why I stated a decimal data type would be preferable.</p> <p>If you want decimal accuracy, that's what you'll have to do. But you might want to consider the cases in that range where Linux goes the other way too (as per your comment) - there may be situation where Linux and Windows disagree in the opposite direction to what you've found - a decimal type probably won't solve that.</p> <p>You may need to make your comparison tools a little more intelligent inasmuch as they can ignore a difference of 1 in the final fractional place.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload