Velocity Reviews > Right-Justifying Numeric Output

# Right-Justifying Numeric Output

hg
Guest
Posts: n/a

 01-25-2007
Rich Shepard wrote:

> I have this print statement in a function:
>
> print '%2d \$%11.2f \$%10.2f \$%9.2f \$%9.2f' %(nper, pv, diff, ten,
> bonus)
>
> and I would like to have the output right justified in the specified
> field. My trials using .rjust(n) have produced that string in the output,
> not the intended results. Is there a way of right justifying numeric
> output?
>
> Thanks,
>
> Rich
>
> --
> Richard B. Shepard, Ph.D. | The Environmental Permitting
> Applied Ecosystem Services, Inc. | Accelerator(TM)
> <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax:
> 503-667-8863

assuming max size = 10

#with spaces
>> '% 10d' % 11

' 11'
#with zeros
>>'%010d' % 11

'0000000011'

hg

Rich Shepard
Guest
Posts: n/a

 01-25-2007
I have this print statement in a function:

print '%2d \$%11.2f \$%10.2f \$%9.2f \$%9.2f' %(nper, pv, diff, ten, bonus)

and I would like to have the output right justified in the specified field.
My trials using .rjust(n) have produced that string in the output, not the
intended results. Is there a way of right justifying numeric output?

Thanks,

Rich

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc. | Accelerator(TM)
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Rich Shepard
Guest
Posts: n/a

 01-25-2007
On Thu, 25 Jan 2007, hg wrote:

> assuming max size = 10
>
> #with spaces
>>> '% 10d' % 11

> ' 11'
> #with zeros
>>> '%010d' % 11

> '0000000011'

Thank you very much! I didn't see this in any of my Python books or on the
Web.

Rich

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc. | Accelerator(TM)
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Tobiah
Guest
Posts: n/a

 01-25-2007

> Thank you very much! I didn't see this in any of my Python books or on
> the

I like to look at the printf docs for any C compiler. On Unix,
man 3 printf

--
Posted via a free Usenet account from http://www.teranews.com

Gabriel Genellina
Guest
Posts: n/a

 01-26-2007
At Thursday 25/1/2007 19:34, Rich Shepard wrote:

> > assuming max size = 10
> >
> > #with spaces
> >>> '% 10d' % 11

> > ' 11'
> > #with zeros
> >>> '%010d' % 11

> > '0000000011'

>
> Thank you very much! I didn't see this in any of my Python books or on the
>Web.

http://docs.python.org/lib/typesseq-strings.html

--
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas