locale.format without trailing zeros

Discussion in 'Python' started by przemolicc@poczta.fm, Aug 22, 2011.

  1. Guest

    Hello,

    >>> import locale
    >>> locale.setlocale(locale.LC_ALL, "pl_PL")

    'pl_PL'
    >>> i=0.20
    >>> j=0.25
    >>> locale.format('%f', i)

    '0,200000'
    >>> locale.format('%f', j)

    '0,250000'

    I need to print the numbers in the following format:
    '0,2' (i)
    '0,25' (j)
    So the last trailing zeros are not printed.

    Regards
    Przemyslaw Bak (przemol)



















































    ----------------------------------------------------------------
    Najwieksza baza najtanszych ofert mieszkaniowych
    http://linkint.pl/f2a0e
    , Aug 22, 2011
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    >>>> import locale
    >>>> locale.setlocale(locale.LC_ALL, "pl_PL")

    > 'pl_PL'
    >>>> i=0.20
    >>>> j=0.25
    >>>> locale.format('%f', i)

    > '0,200000'
    >>>> locale.format('%f', j)

    > '0,250000'
    >
    > I need to print the numbers in the following format:
    > '0,2' (i)
    > '0,25' (j)
    > So the last trailing zeros are not printed.


    >>> print locale.format("%g", 1.23)

    1,23
    >>> print locale.format("%g", 1.2345678)

    1,23457
    >>> print locale.format("%.10g", 1.2345678)

    1,2345678
    >>> print locale.format("%.15g", 0.1)

    0,1
    >>> print locale.format("%.17g", 0.1)

    0,10000000000000001
    Peter Otten, Aug 22, 2011
    #2
    1. Advertising

  3. Anssi Saari Guest

    writes:

    > Hello,
    >
    >>>> import locale
    >>>> locale.setlocale(locale.LC_ALL, "pl_PL")

    > 'pl_PL'
    >>>> i=0.20
    >>>> j=0.25
    >>>> locale.format('%f', i)

    > '0,200000'
    >>>> locale.format('%f', j)

    > '0,250000'
    >
    > I need to print the numbers in the following format:
    > '0,2' (i)
    > '0,25' (j)
    > So the last trailing zeros are not printed.


    That would be the %g conversion specifier.
    Anssi Saari, Aug 22, 2011
    #3
  4. Guest

    On Mon, Aug 22, 2011 at 11:48:46AM +0200, Peter Otten wrote:
    > wrote:
    >
    > >>>> import locale
    > >>>> locale.setlocale(locale.LC_ALL, "pl_PL")

    > > 'pl_PL'
    > >>>> i=0.20
    > >>>> j=0.25
    > >>>> locale.format('%f', i)

    > > '0,200000'
    > >>>> locale.format('%f', j)

    > > '0,250000'
    > >
    > > I need to print the numbers in the following format:
    > > '0,2' (i)
    > > '0,25' (j)
    > > So the last trailing zeros are not printed.

    >
    > >>> print locale.format("%g", 1.23)

    > 1,23
    > >>> print locale.format("%g", 1.2345678)

    > 1,23457
    > >>> print locale.format("%.10g", 1.2345678)

    > 1,2345678
    > >>> print locale.format("%.15g", 0.1)

    > 0,1
    > >>> print locale.format("%.17g", 0.1)

    > 0,10000000000000001


    Thank you very much :)

    Regards
    Przemyslaw Bak (przemol)



















































    ----------------------------------------------------------------
    Znajdz samochod idealny dla siebie!
    Szukaj >> http://linkint.pl/f2a0a
    , Aug 22, 2011
    #4
  5. Guest

    On Mon, Aug 22, 2011 at 11:48:46AM +0200, Peter Otten wrote:
    > wrote:
    >
    > >>>> import locale
    > >>>> locale.setlocale(locale.LC_ALL, "pl_PL")

    > > 'pl_PL'
    > >>>> i=0.20
    > >>>> j=0.25
    > >>>> locale.format('%f', i)

    > > '0,200000'
    > >>>> locale.format('%f', j)

    > > '0,250000'
    > >
    > > I need to print the numbers in the following format:
    > > '0,2' (i)
    > > '0,25' (j)
    > > So the last trailing zeros are not printed.

    >
    > >>> print locale.format("%g", 1.23)

    > 1,23
    > >>> print locale.format("%g", 1.2345678)

    > 1,23457
    > >>> print locale.format("%.10g", 1.2345678)

    > 1,2345678
    > >>> print locale.format("%.15g", 0.1)

    > 0,1
    > >>> print locale.format("%.17g", 0.1)

    > 0,10000000000000001


    How about this format:
    ',1'
    (the local zero is also not printed)

    (I know this is strange but I need compatibility with local requirements)

    Regards
    Przemyslaw Bak (przemol)



















































    ----------------------------------------------------------------
    Najwieksza baza najtanszych ofert mieszkaniowych
    http://linkint.pl/f2a0e
    , Aug 22, 2011
    #5
  6. Peter Otten Guest

    wrote:

    > How about this format:
    > ',1'
    > (the local zero is also not printed)
    >
    > (I know this is strange but I need compatibility with local requirements)


    I believe you have to do it yourself:

    >>> locale.format("%f", 0.123)

    '0,123000'
    >>> locale.format("%f", 0.123).strip("0")

    ',123'
    Peter Otten, Aug 22, 2011
    #6
    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. Michael

    Printing double with no trailing zeros

    Michael, Nov 14, 2003, in forum: C Programming
    Replies:
    3
    Views:
    4,652
    Martin Ambuhl
    Nov 14, 2003
  2. BARBARA FLOYD

    ASP IN EXCEL, trailing zeros truncated

    BARBARA FLOYD, Nov 11, 2003, in forum: ASP General
    Replies:
    1
    Views:
    154
    Ray at
    Nov 11, 2003
  3. ThePotPlants

    Number padding... (trailing zeros'.)

    ThePotPlants, May 23, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    369
    ThePotPlants
    May 23, 2004
  4. Replies:
    4
    Views:
    108
  5. Replies:
    4
    Views:
    88
    PyNoob
    Jun 27, 2013
Loading...

Share This Page