float to string with different precision

Discussion in 'Python' started by zunbeltz@gmail.com, Aug 10, 2007.

  1. Guest

    Hi,

    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.

    Thanks in advance for the help,

    Zunbeltz
    , Aug 10, 2007
    #1
    1. Advertising

  2. Peter Otten Guest

    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
    Peter Otten, Aug 10, 2007
    #2
    1. Advertising

  3. Zentrader Guest

    On Aug 10, 1:12 am, Peter Otten <> wrote:
    > 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]
    Zentrader, Aug 10, 2007
    #3
  4. Roger Miller Guest

    On Aug 10, 8:37 am, Zentrader <> wrote:
    >
    > 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]


    This has the disadvantage that it doesn't round the last digit. For
    example 10.356634 yields 10.356 instead of 10.357.

    You can use '*' in format strings to take a numeric field value from a
    variable. For example

    ndecimals = 2
    print "%5.*f" % (ndecimals, x)

    formats x with 2 digits following the decimal point. Or you can
    simply
    cobble up a format string at run time:

    format = "%%5.%df" % ndecimals
    print format % x
    Roger Miller, Aug 11, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thinkit
    Replies:
    2
    Views:
    292
    Thinkit
    Nov 28, 2003
  2. Bryan R. Meyer

    Precision Change - Float to String

    Bryan R. Meyer, May 21, 2004, in forum: C++
    Replies:
    5
    Views:
    3,694
    E. Robert Tisdale
    May 22, 2004
  3. bd
    Replies:
    0
    Views:
    604
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,504
    James Kanze
    Oct 8, 2009
  5. Anton81

    Float precision and float equality

    Anton81, Dec 5, 2009, in forum: Python
    Replies:
    26
    Views:
    1,115
Loading...

Share This Page