Python Forum

Full Version: Formatting $
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Using this formatting code:

print('${:>10,.0f}'.format(xyz))


I get:
$        1,234
$12,000,000

I want:
        $1,234
      $15,000
$12,000,000

1. How do change the format statement to get the $ to be up against the first number
Something like this.
>>> xyz = 1234
>>> print('{:>5}${:,.0f}'.format('', xyz))
    $1,234
>>>
Hmm look like bug in code tag.
So it should be     $1,234
thank you -  will give it a try

Well, the change did move the $ to the first number as wanted, but when in a column of numbers of different lengths, it was not right justified -- in fact, it was left justified.  for example, here is how the output came out:

$1,234
$1,234,567

Is there a way to right justify?
Can you post your code,so we can run it.
I am very very new to Python, so don't laugh at my code.  Plus, I looked at this site for "code tags" but could not find out how to properly post -- so I am just going to copy and paste.  Part 2 of my code is where I talk about the column of numbers not right justifying. Part 3 generates an error as you will see when it is run -- I was going to work on that after I fixed Part 2.  thanks for helping (how do I use the code tags so when I copy and paste it comes out correct?)
# Part 1
your_start_age = 62
her_start_age = 61
start_year = 2017

taxableequity = 100000
taxablefixedincome = 200000
taxablecash = 50000
taxableequitygwth = 5
taxablefixedincomediv = 6
taxablecashdiv = 4


# Part 2
f = open("test.txt", "w")
print("Year", "\t", "Your", "\t", "Her", "\t", "Taxable", "\t", "Taxable", "\t", "Taxable")
print("\t", "\t", "Age", "\t", "Age", "\t", "Equity", "\t", "Fixed Inc", "\t", "Cash")

for y in range(81):
   print(start_year, "\t", your_start_age, "\t", her_start_age, "\t", '{:>5}${:,.0f}'.format('',taxableequity), "\t", '${:>5,.0f}'.format(taxablefixedincome), "\t", '${:>5,.0f}'.format(taxablecash))
   f.write('{} {} {} {} {} {} {} {} {}\n'.format(start_year, your_start_age, her_start_age, taxableequity, taxablefixedincome, taxablecash, taxableequitygwth, taxablefixedincomediv, taxablecashdiv))
   your_start_age += 1
   her_start_age += 1
   start_year += 1
   taxableequity = taxableequity * (1+ (taxableequitygwth/100))
   taxablefixedincome = taxablefixedincome * (1 + (taxablefixedincomediv/100))
   taxablecash = taxablecash * (1 + (taxablecashdiv/100))
f.close()

# Part 3
print("Year", "\t", "Your", "\t", "Her", "\t", "Taxable", "\t", "Taxable", "\t", "Taxable")
print("\t", "\t", "Age", "\t", "Age", "\t", "Equity", "\t", "Fixed Income", "\t", "Cash")

with open("test.txt", "r") as opened_file:
   for line in opened_file:
       start_year, your_start_age, her_start_age, taxableequity, taxablefixedincome, taxablecash, taxableequitygwth, taxablefixedincomediv, taxablecashdiv = line.split()
       print(start_year, "\t", your_start_age, "\t", her_start_age, "\t", '${:,.0}'.format(taxableequity), "\t",'${:,.0}'.format(taxablefixedincome), "\t", '${:,.0}'.format(taxablecash))
It's ugly but works.

>>> '{}'.format(('$' + str(xyz)).rjust(10, ' '))
'    $1,234'
>>> 
You can just drop the str.format() method and use only ('$' + str(xyz)).rjust(10, ' ') part
Hah. the '$1' is missing from the code :D
I am confused (again I am a beginner).  When I use:

'{}'.format(('$'  + str(taxableequity)).rjust(10,' '))


it does not round to 0 places -- I get 5-10 digits right of the decimal point --  i need it to round to no decimal places.
>>> x = 1.22232424252552
>>> round(x)
1
>>> round(x,2)
1.22
>>> 
(Dec-22-2016, 05:08 PM)birdieman Wrote: [ -> ]I am confused (again I am a beginner).  When I use:

'{}'.format(('$'  + str(taxableequity)).rjust(10,' '))
You can do adjust and decimal place in one go.
Eg.
>>> taxableequity = 1.2345678
>>> '{:{}{}.{}}'.format(taxableequity, '>', 10, 2)
'       1.2'
For more about formatting look at these links PyformatString Formatting Cookbook.

There is a BBCode help button for how to use code tag.
I am new to python and am having trouble formatting a variable.  I read several variables from a file, print them in columns, and would like certain columns to come out like this:

             $12
         $1,234
 $12,000,000

Right justified, $ next to first number, commas inserted, no decimals (not lopped off, just rounded to nearest $)

here is the couple of lines of code that reads the file (all numbers), prints headers, and prints the variables.  Notice that I have tried two different ways to format (again, I am just learning, so I am just trying stuff).  The read part works -- I get output -- it is just not formatted.  I will post entire code if someone wants.
print("Year", "\t", "Your", "\t", "Spouse", "\t", "Taxable", "\t", "Taxable", "\t", "Taxable")
print("\t", "\t", "Age", "\t", "Age", "\t", "Equity", "\t", "Fixed Income", "\t", "Cash")

with open("test.txt", "r") as opened_file:
    for line in opened_file:
        start_year, your_start_age, her_start_age, taxableequity, taxablefixedincome, taxablecash, taxableequitygwth, \
        taxablefixedincomediv, taxablecashdiv = line.split()
        print(start_year, "\t", your_start_age, "\t", her_start_age, "\t", '{}'. format('$' + str(taxableequity)), "\t",
              '{:{}{}.{}}'.format(taxablefixedincome, '>', 10, 10), "\t", taxablecash)
What do I change in either method to achieve my desired results?  Thanks
Pages: 1 2