insert thousands separators

Gerhard Häring gh at ghaering.de
Mon Jun 16 10:00:42 EDT 2003


Nick Vargish wrote:
> ps. Looking at the library reference, it's not clear if there's an easy
> way to extract the fractional portion of a float without hitting the
> Standard Floating Point Problem:
> 
>>>>v = 2312.3123123
>>>>v1 = int(v)
>>>>v2 = v - v1
>>>>v2
> 
> 0.31231230000003052

If you need that, then a float is probably not the right data type. 
There are several implementations of arbitrary precision numbers out there.

Some more fun with floats:

#v+
 >>> v - v1
0.31231230000003052
 >>> s = str(v)
 >>> s
'2312.3123123'
 >>> s[s.find("."):]
'.3123123'
 >>> float(s[s.find("."):])
0.31231229999999999
#v-

So if you only want to print the fractional value, str() will do fine. 
But str() won't make floats capable of representing arbitrary numbers, 
either ;-)

-- Gerhard





More information about the Python-list mailing list