Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > locale.format without trailing zeros

Reply
Thread Tools

locale.format without trailing zeros

 
 
przemolicc@poczta.fm
Guest
Posts: n/a
 
      08-22-2011
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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      08-22-2011
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.234567

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

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

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

0,10000000000000001

 
Reply With Quote
 
 
 
 
Anssi Saari
Guest
Posts: n/a
 
      08-22-2011
(E-Mail Removed) 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.
 
Reply With Quote
 
przemolicc@poczta.fm
Guest
Posts: n/a
 
      08-22-2011
On Mon, Aug 22, 2011 at 11:48:46AM +0200, Peter Otten wrote:
> (E-Mail Removed) 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.234567

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

> 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
 
Reply With Quote
 
przemolicc@poczta.fm
Guest
Posts: n/a
 
      08-22-2011
On Mon, Aug 22, 2011 at 11:48:46AM +0200, Peter Otten wrote:
> (E-Mail Removed) 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.234567

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

> 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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      08-22-2011
(E-Mail Removed) 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'

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
format a number with leading and trailing zeros absmienk@hotmail.com Perl Misc 4 08-15-2007 01:18 PM
Formatting date without leading zeros Tom Reinhart Ruby 10 05-15-2005 06:51 PM
Number padding... (trailing zeros'.) ThePotPlants Perl Misc 4 05-23-2004 10:19 PM
Printing double with no trailing zeros Michael C Programming 3 11-14-2003 04:50 AM
ASP IN EXCEL, trailing zeros truncated BARBARA FLOYD ASP General 1 11-11-2003 09:18 PM



Advertisments