[python-win32] working with floats
Ulrich Eck
ueck@net-labs.de
Tue, 7 Aug 2001 22:41:13 +0200
>
> import struct
> struct.pack('f', (round(4,23452345, 1)))
>
> on both boxes you'll get the same thing on each. I don' t think that the
> docs for str() explicitly say exactly how double precision floats will
> be formatted so this is really neither bug nor feature just ambiguity.
>
ok the problem is not the platform but the actual float (see below)
how can i mathematically correct get a float with 2 decimals
without going through a "%.2f" with all my numbers ??
thanks for your answer
Ulrich Eck
see the difference in my examples ..
WIN:
> > >>> f = 4.23452345
> > >>> round(f,1)
> > 4.2000000000000002
LINUX:
> > >>> f = 4.112345234
> > >>> round(f,1)
> > 4.1
I used different random floats to test .. I know have tried it on
both with the same and there is the same result actually.
WIN:
PythonWin 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (MarkH@ActiveState.com) - see 'Help/About PythonWin' for
further copyright information.
>>> f = 4.2345234534
>>> round(f,2)
4.2300000000000004
>>> import struct
>>> struct.pack('f',(round(f,1)))
'ff\x86@'
LINUX:
Python 2.1.1 (#1, Aug 5 2001, 18:08:07)
[GCC 2.95.3 20010315 (release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> f = 4.2345234534
>>> round(f,2)
4.2300000000000004
>>> import struct
>>> struct.pack('f',(round(f,1)))
'ff\x86@'