-1

i am writing a program that will calculate the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day and continues to double each day. i have everything done but i have to get into dollars and i do not know how to do it float only gives me .0 when i need .00

thanks

# Ryan Beardall Lab 8-1 10/31/13
#program calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day and continues to double each day. 
accumulatedPay = []
payPerDay = []
days = []
#User enters days worked to find out ho much money user will have
def numberDays():
    global daysWorked
    daysWorked = (input("Enter the days worked "))
    return daysWorked
def salaryOnDay(daysWorked):
    return float((2**(daysWorked-1))/100)
def calculations():
    earnings = 0
    for i in range (1, daysWorked + 1):
        salary = salaryOnDay(i)       
        currRow = [0, 0, 0]
        currRow[0] = i
        currRow[1] = salary
        earnings += salary
        currRow[2] = earnings
        days.append(currRow)
#program prints matrix
def main():
    numberDays()
    calculations()   
    for el in days:
        print el
main()
martineau
  • 119,623
  • 25
  • 170
  • 301
ryan b
  • 7
  • 1
  • 2

5 Answers5

3

Try this:

for el in days:
    print '{:.2f}'.format(el)

This documentation describes string formatting in more detail.

If the version of Python you are using is too old for this (<2.7, I think), try:

print '{0:.2f}'.format(el)

If the version of Python you are using is too old for that (<2.6, I think), try:

print "%.2f"%el
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • 2
    What version of Python are you using? What error did you get? How did you resolve (or how did you attempt to resolve) that error? – Robᵩ Oct 31 '13 at 13:53
3

Alternatively you could use currency formatting

>>> import locale
>>> locale.setlocale( locale.LC_ALL, '' )
'English_United States.1252'
>>> locale.currency( 188518982.18 )
'$188518982.18'
>>> locale.currency( 188518982.18, grouping=True )
'$188,518,982.18'

(Courtesy S.Lott)

Community
  • 1
  • 1
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
1

I'm doing the same problem, just a little more simply by defining the float format as 0.01 to start with:

def main():
    num_input = int(input('How many days do you have? '))
    # Accumulate the total
    total = 0.01
    for day_num in range(num_input):
        if day_num == 0:
            print("Day: ", day_num, end=' ')
            total = total
            print("the pennies today are:", total)
            day_num += day_num
        else:    
            print("Day: ", day_num + 1, end=' ')
            total += 2 * total
            print("the pennies today are:", total)         
main()

Output: How many days do you have? 5
Day: 0 the pennies today are: 0.01
Day: 2 the pennies today are: 0.03
Day: 3 the pennies today are: 0.09
Day: 4 the pennies today are: 0.27
Day: 5 the pennies today are: 0.81

Enber
  • 13
  • 2
1

I did it this way

P = 0.01
ttlP = P
dys = int(input('How many days would you like to calculate?'))
for counter in range(1, dys + 1):
  print('Day',counter,'\t \t \t $',P)
  P = P * 2
  ttlP = ttlP + P
ttlP = ttlP - P
print('Total pay is $',format((ttlP),'.2f'),sep='')
-1

Redid this example for C#. Just using the default console application settings from visual studio to get started.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW_1_Penny_Doubling
{
    class Program
    {
        static void Main(string[] args)
        {
            double total = 0.01;
            Console.WriteLine("How many completed days will you be working?");
            int days = int.Parse(Console.ReadLine());

            if(days <= 200)
            {
                for(int i = 1; i <= days; i++)
                {
                    if (days == 0)
                    {
                        Console.WriteLine("At zero days, you won't even make a penny");
                    }
                    else
                    {
                        Console.WriteLine("Day: " + i);
                        total = total + (2 * total); // Take the total and add double that to the original amount
                        Console.WriteLine("Today's total is: " + total);
                    }
                }

            }
            else
            {
                Console.WriteLine("The value will be too high, Try a number below 200");

            }
        }
    }
}
Enber
  • 13
  • 2