Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #12001 > unrolled thread
| Started by | przemolicc@poczta.fm |
|---|---|
| First post | 2011-08-22 10:52 +0200 |
| Last post | 2011-08-22 13:59 +0300 |
| Articles | 6 — 3 participants |
Back to article view | Back to comp.lang.python
locale.format without trailing zeros przemolicc@poczta.fm - 2011-08-22 10:52 +0200
Re: locale.format without trailing zeros Peter Otten <__peter__@web.de> - 2011-08-22 11:48 +0200
Re: locale.format without trailing zeros przemolicc@poczta.fm - 2011-08-22 13:08 +0200
Re: locale.format without trailing zeros przemolicc@poczta.fm - 2011-08-22 13:29 +0200
Re: locale.format without trailing zeros Peter Otten <__peter__@web.de> - 2011-08-22 15:01 +0200
Re: locale.format without trailing zeros Anssi Saari <as@sci.fi> - 2011-08-22 13:59 +0300
| From | przemolicc@poczta.fm |
|---|---|
| Date | 2011-08-22 10:52 +0200 |
| Subject | locale.format without trailing zeros |
| Message-ID | <mailman.303.1314005406.27778.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-08-22 11:48 +0200 |
| Message-ID | <j2t8lo$99c$1@solani.org> |
| In reply to | #12001 |
przemolicc@poczta.fm 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
[toc] | [prev] | [next] | [standalone]
| From | przemolicc@poczta.fm |
|---|---|
| Date | 2011-08-22 13:08 +0200 |
| Message-ID | <mailman.308.1314011326.27778.python-list@python.org> |
| In reply to | #12003 |
On Mon, Aug 22, 2011 at 11:48:46AM +0200, Peter Otten wrote:
> przemolicc@poczta.fm 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
[toc] | [prev] | [next] | [standalone]
| From | przemolicc@poczta.fm |
|---|---|
| Date | 2011-08-22 13:29 +0200 |
| Message-ID | <mailman.310.1314012601.27778.python-list@python.org> |
| In reply to | #12003 |
On Mon, Aug 22, 2011 at 11:48:46AM +0200, Peter Otten wrote:
> przemolicc@poczta.fm 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
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-08-22 15:01 +0200 |
| Message-ID | <j2tjvp$ekp$1@solani.org> |
| In reply to | #12011 |
przemolicc@poczta.fm 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'
[toc] | [prev] | [next] | [standalone]
| From | Anssi Saari <as@sci.fi> |
|---|---|
| Date | 2011-08-22 13:59 +0300 |
| Message-ID | <vg339gt7lud.fsf@coffee.modeemi.fi> |
| In reply to | #12001 |
przemolicc@poczta.fm 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web