How to round to the nearest lower float in Python?
<p>I have a list of floats which I want to round up to 2 numbers; I used below line for this purpose:</p>

<pre><code>item = ['41618.45110', '1.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '0.00', '41619.001202', '3468.678822']
print ["{0:.2f}".format(round(float(x), 2)) if re.match("^\d+(\.\d+)?$", x) else x for x in item]
</code></pre>

<p>It rounds all the list members to the nearest upper float which causes <code>3468.678822</code> to be rounded to <code>3468.68</code>, but I want to round them to the nearest lower float, so <code>3468.678822</code> should be rounded to <code>3468.67</code>. There is an exception for <code>0</code>; I want numbers equal to <code>0</code> to remain <code>0</code>.</p>

<p>I tried using above command without <code>round</code> and even <code>float</code> function and the result was the same. I also tried: </p>

<pre><code>[x[:x.index('.')] if re.match("^\d+(\.\d+)?$", x) else x for x in item]
</code></pre>

<p>Which gave me <code>Substring not found</code> error.</p>

<p>You can use a cast to do that :</p>

<pre><code>a = '3468.678822'

def round_2(n):
    return ((int)(n*100)/100)

print(round_2(float(a)))

&gt;&gt;&gt; 3468.67 
</code></pre>

python formatting precision