-2

I can't get code to round to the second decimal

Ive tried changing from str to int and even float? print(round(GrossPay,2))

#SHORT TERM CALCULATOR

rate_of_pay = (input("what is the partners rate of pay? $"))

#SALARY CALCULATIONS 100% 5 DAY WORK WEEK
salary_weekly = float(rate_of_pay)*40
salary_daily = salary_weekly/5

#HOURLY CALCULATION 66.67% 7 DAY WORK WEEK
hourly_cal = float(rate_of_pay)*40
hourly_weekly = hourly_cal*.6667
hourly_daily = hourly_weekly/7

#SALARIED TOTALS
print ("Salaried WEEKLY = $" + str(salary_weekly))
print ("Salaried DAILY = $" + str(salary_daily))

#HOURLY TOTALS
print ("Hourly WEEKLY = $" + str(hourly_weekly))
print ("Hourly DAILY = $" + str(hourly_daily))

Expecting output to show decimal rounded to the second decimal in every situation

Carlee
  • 1
  • 1
  • 2
    You probably want to take a look at the [string formatting](https://docs.python.org/3/library/string.html#format-string-syntax) section of the Python docs. You probably want something like `print("Salaried WEEKLY = ${0:.2f}".format(salary_weekly))`. – John Szakmeister May 02 '19 at 01:16
  • 1
    Possible duplicate of [Limiting floats to two decimal points](https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – Craig May 02 '19 at 01:18
  • Duplicate https://stackoverflow.com/questions/6149006/display-a-float-with-two-decimal-places-in-python – Grismar May 02 '19 at 01:25

3 Answers3

1
print("{:.2f}".format(number))
enzo
  • 1,333
  • 1
  • 6
  • 17
0

You probably want to use

print("${:#.2f}".format(value))

This only works in Python 3 and ensures that the value is always printed with 2 decimals, even if they are 0.

1313e
  • 728
  • 4
  • 15
-1

Use the formatted string print (f'{hourly_daily:.2f}'

Satya
  • 21
  • 1