Two decimal places

Hi there,

This script works fine but when it writes the answer to the file, it only uses one decimal place. How can I specify two decimal places?

# -*- encoding: utf-8 -*-

from sys import argv
script, answer = argv

def salary(weeks, payments):
	return (weeks * payments)

weeks = (float(raw_input("Weeks: ")))
payments = (float(raw_input("Payments: ")))

total = salary(weeks, payments)

target = open(answer, 'w')
target.truncate()

target.write (str(total))

target.close()

Any help really appreciated.
Kind Regards

You might find this a useful resource in general for string formatting: https://pyformat.info/ - this should have the information you’ll need, and more, to change that str(total) into more precisely what you want

In general though, if you’re dealing with values that must be represented exactly, like money, then you shouldn’t be using floats to represent them

a common trick with currencies that have cents and pence etc is to track all values in integer numbers of pence rather than pounds, or cents rather than dollars

Alternatively there are libraries including decimal that can deal with this more robustly

Thanks so much. I will look through it.

I should probably point out for completeness that there’s a built in function, round, that can actually round the number if you want to use the rounded number for other things

In this case, round(total, 2)

round

doesn’t work for me. I get Invalid Syntax on that line.

# -*- encoding: utf-8 -*-

from sys import argv
script, answer = argv

def salary(weeks, payments):
	return (weeks * payments)

weeks = (float(raw_input("Weeks: ")))
payments = (float(raw_input("Payments: ")))

total = salary(weeks, payments)

target = open(answer, 'w')
target.truncate()

target.write round(total, 2)

target.close()

Running in Terminal gives me this.

MacBook-Air:python jasonnewton$ python question.py answer.txt
  File "question.py", line 18
    target.write round(total, 2)
                     ^
SyntaxError: invalid syntax

You’re missing the parentheses. target.write(round(total, 2))

Thanks, @chuckadams I thought that would do it but it still brings up an error in Terminal.

File "question.py", line 17, in <module>
    target.write(round(total, 2))
TypeError: expected a character buffer object

Part of debugging is searching for error messages

The .write method expects, as the message says, a character buffer, which is not interchangeable with numbers. If you want to write the string representation of a number you need to convert it to bytes. The answer to that part is a little less obvious, but converting to a string then encoding it to bytes should do it:

target.write(str(round(total, 2)).encode())

Thanks so much, @chuckadams

This works perfectly. I’m only just starting to learn Python so some of the explanation is a bit beyond my understanding right now. I’m only just getting to grips with simple loops and lists - working from a book on Python.

Anyway, I appreciate your time on this.

Kind Regards