Result of multiplication and division with two houses after the comma

2

Hello everyone, how are you?

I'm doing an activity for my python course, it's a script that basically calculates the area of a wall in meters and tells how many gallons will be used for paint. The problem is that when I type a very high value of height and width, type, 543.404 x 456.435 the script returns me the following result area 2.3e + 05, I wanted all results to come with floating points and only two houses after the comma, follow my code. Note: in my program 1 liter of paint paints 2m2 of area, so the variable t = 2.

Obs2: Someone helps me to post the code correctly here on the site.

print('Bem vindo ao programa de orçamento\nde tintas: ')

l = float(input('Digite a largura da parede em metros: '))

print('Muito bem!')

a = float(input('Agora digite a altura da parede: '))

m = (l * a)

print ('Certo! Você tem uma área de: {:.2} metros' .format (l * a))

t = 2

print ('Para pintar esta parede, você vai\nprecisar de: {:.2} litros de tinta!'.format(m / t))
    
asked by anonymous 19.06.2018 / 01:06

2 answers

3

This is because, by default, when printing floats like this, Python tries to use scientific notation to shorten the result.

You can force it to give you the result in full using {:.2f} instead of just {:.2} :

print('Certo! Você tem uma área de: {:.2f} metros'.format (l * a))
t = 2
print('Para pintar esta parede, você vai\nprecisar de: {:.2f} litros de tinta!'.format(m / t))
    
19.06.2018 / 01:20
2

You can use the round function to round the values, then display the values correctly with the print function. The characters that appear are the values in scientific notation.

print('Bem vindo ao programa de orçamento\nde tintas: ')

l = float(input('Digite a largura da parede em metros: '))

print('Muito bem!')

a = float(input('Agora digite a altura da parede: '))

m = round((l * a),2)

print ('Certo! Você tem uma área de: ',m,'metros')

t = 2
litros =round( (m/t) , 2)

print ('Para pintar esta parede, você vai\nprecisar de: ',litros,' litros de tinta!')
    
19.06.2018 / 01:23