Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems like you were hoping that converting the float to a 2-decimal-place string and then back to a float would give you a 2-decimal-place float.</p> <p>The first problem is that your code doesn't actually do that anywhere. If you'd done that, you would get something very close to <code>7.65</code>, not <code>7.65321</code>.</p> <p>But the bigger problem is that what you're trying to do doesn't make any sense. A float always has 53 binary digits, no matter what. If you round it to two decimal digits (no matter how you do it, including by converting to string and back), what you actually get is a float rounded to two decimal digits <em>and then rounded to 53 binary digits</em>. The closest <code>float</code> to <code>7.65</code> is not exactly <code>7.65</code>, but <code>7.650000000000000355271368</code>.* So, that's what you'd end up with. And there's no way around that; it's inherent to the way <code>float</code> is stored.</p> <p>However, there is a <em>different</em> type you can use for this: <a href="http://docs.python.org/2/library/decimal.html" rel="nofollow"><code>decimal.Decimal</code></a>. For example:</p> <pre><code>&gt;&gt;&gt; f = 7.65321 &gt;&gt;&gt; s = '%.2f' % f &gt;&gt;&gt; d = decimal.Decimal(s) &gt;&gt;&gt; f, s, d (7.65321, '7.65', Decimal('7.65')) </code></pre> <p>Or, of course, you could just pass around a string instead of a float (as you're accidentally doing in your code already), or you could remember to use the <code>.2f</code> format every time you want to output it.</p> <hr> <p>As a side note, since your <code>DNumber</code> ends up as a string, this line is not doing anything useful:</p> <pre><code>if DNumber &lt;= 8: </code></pre> <p>In Python 2.x, comparing two values of different types gives you a consistent but arbitrary and meaningless answer. With CPython 2.x, it will always be <code>False</code>.** In a different Python 2.x implementation, it might be different. In Python 3.x, it raises a <code>TypeError</code>.</p> <p>And changing it to this doesn't help in any way:</p> <pre><code>if DNumber &lt;= float(8): </code></pre> <p>Now, instead of comparing a <code>str</code> to an <code>int</code>, you're comparing a <code>str</code> to a <code>float</code>. This is exactly as meaningless, and follows the exact same rules. (Also, <code>float(8)</code> means the same thing as <code>8.0</code>, but less readable and potentially slower.)</p> <p>For that matter, this:</p> <pre><code>if DNumber: </code></pre> <p>… is always going to be true. For a number, <code>if foo</code> checks whether it's non-zero. That's a bad idea for <code>float</code> values (you should check whether it's within some absolute or relative error range of 0). But again, you don't have a <code>float</code> value; you have a <code>str</code>. And for strings, <code>if foo</code> checks whether the string is non-empty. So, even if you started off with <code>0</code>, your string <code>"0.00"</code> is going to be true.</p> <hr> <p>* I'm assuming here that you're using CPython, on a platform that uses IEEE-754 double for its C <code>double</code> type, and that all those extra conversions back and forth between string and float aren't introducing any additional errors.</p> <p>** The rule is, slightly simplified: If you compare two numbers, they're converted to a type that can hold them both; otherwise, if either value is <code>None</code> it's smaller; otherwise, if either value is a number, it's smaller; otherwise, whichever one's type has an alphabetically earlier name is smaller.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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