-1

I'm creating a piece of software for my graduation and I want to have a better end-result I just wish to show the values: 66.66, 3.70, and 296.296. But in my calculations the software is showing me the values as:

66.66666666666667
3.703703703703704 
296.2962962962963

What should I change in the code? Another day someone taught me how to use 2%f but I believe I made mistakes with it.

Could you help?

quantidade_lata = float(18)
valor_lata =  float(80)
litros_de_tinta = float  (3)

print (' Olá bem vindo a loja de tintas, vamos começar com algumas perguntas basicas antes do seu orçamento')
print ('Lembrando que cada lata tem 18 litros e cada 1 litro pinta até 3 metros')
user_information1 = float (input("Quantos metros você vai pintar ?"))

print ('Você ira precisar de :',user_information1 / litros_de_tinta, 'litros de tinta')
latas_necessarias = (user_information1 /  litros_de_tinta) / quantidade_lata
print ('Você ira precisar de ',latas_necessarias,'sendo R$ 80,00 CADA lata,pressione ENTER para ver o valor do orçamento ')
input()
print   ('O valor a pagar em R$ é :',latas_necessarias * valor_lata)
baduker
  • 19,152
  • 9
  • 33
  • 56
  • 6
    I really do not get why well documented functions with thousends of hits on google and tutorials get to pop up here all the time: https://docs.python.org/3.4/library/string.html#formatspec Please read [how to ask](https://stackoverflow.com/help/how-to-ask) and pay special attention to **"research first, ask later"**. Thanks. https://pyformat.info/ h – Patrick Artner Mar 11 '18 at 16:05
  • 7
    Possible duplicate of [Limiting floats to two decimal points](https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points) – Rolf of Saxony Mar 11 '18 at 16:10

3 Answers3

0
"%.2f" % 1.2399 # returns "1.24"
"%.3f" % 1.2399 # returns "1.240"
"%.2f" % 1.2 # returns "1.20"
Ethan
  • 163
  • 2
  • 12
0

For showing two digits you could, i.e. replace the following line

print ('Você ira precisar de :',user_information1 / litros_de_tinta, 'litros de tinta')

with

print ('Você ira precisar de : {:.2f} litros de tinta'.format(user_information1 / litros_de_tinta))

As you can see, I combined your both text strings and put a {:.2f} at the position where the number shall be. The :.2f shows that you want 2 numbers after the dot. In the format parameter list you then put the number you want to show.

For a more extensive explanation I recommend looking at this website.

Franz
  • 49
  • 6
0

Here is your fixed code:

quantidade_lata = float(18)
valor_lata =  float(80)
litros_de_tinta = float  (3)

print (' Olá bem vindo a loja de tintas, vamos começar com algumas perguntas basicas antes do seu orçamento')
print ('Lembrando que cada lata tem 18 litros e cada 1 litro pinta até 3 metros')
user_information1 = float (input("Quantos metros você vai pintar ?"))

print ('Você ira precisar de {:.2f}: litros de tinta'.format(user_information1 / litros_de_tinta))
latas_necessarias = (user_information1 /  litros_de_tinta) / quantidade_lata
print ('Você ira precisar de {:.2f} ,sendo R$ 80,00 CADA lata,pressione ENTER para ver o valor do orçamento'.format(latas_necessarias))
input()
print   ('O valor a pagar em R$ é : {:.2f}'.format(latas_necessarias * valor_lata))

This is how the output looks:

Olá bem vindo a loja de tintas, vamos começar com algumas perguntas basicas antes do seu orçamento Lembrando que cada lata tem 18 litros e cada 1 litro pinta até 3 metros Quantos metros você vai pintar ?5 Você ira precisar de 1.67: litros de tinta Você ira precisar de 0.09 ,sendo R$ 80,00 CADA lata,pressione ENTER para ver o valor do orçamento

O valor a pagar em R$ é : 7.41

WorkShoft
  • 440
  • 6
  • 18