0

I am working on a practice exercise using Python 3.7.2. The program takes the input, total sales in this problem, and calculates and display the total profit.

Is there a way to pre-format the input float so looks more like currency, with a dollar sign, commas separating the thousands, and two digits at the end for cents?

Currently, the program works fine as long as I enter any given integer.

The output appears just like it should, but the input just appears as a large row of numbers.

The input should appear as $23,987,456.00.

def main():
    display_message()

    # Get sales
    total_sales = float(input("Enter the total sales: "))

    # Calculate annual profit
    total_profit = total_sales * annual_profit
    display_results(total_profit)

def display_message():
    print("")
    print("This program determines the annual profit from the total sales, and")
    print("displays the projected amount.")
    print("")

def display_results(total_profit):
    print("")
    print('The total profit is: $', format(total_profit, '6,.2f'))

# Define global constants and call main
annual_profit = float(0.23)
main()
Aneesh Palsule
  • 337
  • 2
  • 12
  • Can you give a sample input and output that aren't working how you like? – Mad Physicist Jan 25 '19 at 04:02
  • https://stackoverflow.com/questions/320929/currency-formatting-in-python – Jagadesha NH Jan 25 '19 at 04:03
  • [The `decimal` module recipes](https://docs.python.org/3/library/decimal.html#recipes) include one for formatting money outputs. You should definitely be using `decimal` over `float` for financial calculations. – ShadowRanger Jan 25 '19 at 04:04
  • To be clear, do you want to format your output, or parse input? The former is covered by the suggested duplicate, but your wording seems to lean towards the latter. – ShadowRanger Jan 25 '19 at 04:06
  • Here is an output from the program. This program determines the annual profit from the total sales, and displays the projected amount. Enter the total sales: 23987456 The total profit is: $ 5,517,114.88 Ideally, when I enter the total sales 23987456, I would like to see it appear on the screen as $ 23,987,456.00. – Christopher Ranes Jan 25 '19 at 04:08
  • i already said use locale to format your input – ravishankar chavare Jan 25 '19 at 05:05

0 Answers0