Home

On Aug 10, 1:12 am, Peter Otten <__pete...@web.de> wrote:
> zunbe...@gmail.com wrote:
> > I have to print float numbers to a file. Each float should be 5
> > characters in width (4 numbers and the decimal point).
> > My problem is that I do not now how to specify float to have different
> > numbers of decimals. For example
>
> > 5.32 -> 5.320
> > 10.356634 -> 10.357
> > 289.234 -> 289.2
>
> > In the string formating operations only fixed number of decimal digits
> > is allow.
> >>> ["%#.4g" % f for f in [5.32, 10.356634, 289.234, 123456789.]]
>
> ['5.320', '10.36', '289.2', '1.235e+08']
>
> Found by playing around with format strings, so no guarantees.
>
> Peter

If the above does not work
[/code]test_list = [ 5.32, 10.35634, 289.234 ]
for num in test_list :
str_num = "%11.5f" % (num) ## expand to at least 5
print str_num, "-->", str_num.strip()[:5][/code]

previous
next

Re: seeking tinter module
Re: Dictionary viewer and editor
Re: A malloc error in C++ - incorrect checksum for freed object
Re: I am new to programming and wanted to know where a good placetostart for C++
Re: Parser Generator?
Nasze Dzieci
Fundacja Hobbit
Mam Marzenie
Fundacja Avalon
Akogo