How to specify different precision for different values in python

Asked

Viewed 83 times

0

Hello, I am trying to print values with different decimal accuracies, but even after attempts and researches, I could not find a solution. The code snippet with this problem is:

    valor_monetario = round(float(input()), 2)
    moedas = [1, 0.50, 0.25, 0.10, 0.05, 0.01]
    for valor in moedas:
        qtde = int(valor_monetario / valor)
        valor_monetario = valor_monetario % valor
        print('{0} moeda(s) de R$ {0:.2f}'.format(round(qtde), round(valor, 2)))

the output for monetary value = 576.73 is this:

1 currency(s) of R$ 1.00
1 currency(s) of R$ 1.00
0 currency(s) of R$ 0.00
2 currency(s) of R$ 2.00
0 currency(s) of R$ 0.00
3 currency(s) of R$ 3.00

but it should be:

1 currency(s) of R$ 1.00
1 currency(s) of R$ 0.50
0 currency(s) of R$ 0.25
2 currency(s) of R$ 0.10
0 currency(s) of R$ 0.05
3 currency(s) of R$ 0.01

0.50, for example, is rounded to 1.00. If I simply print the isolated value in the form below (which is exactly as it is in the print above, but in isolation), it displays the value with the correct accuracy:

    print('{0:.2f}'.format(round(valor, 2)))

How can I print in a single print, values with different accuracies? Thank you.

  • what is the monetary value variable?

  • Hi, it is an input value. The output shown has the monetary value with the value 576.73.

  • I split it with //, but it came out the same @Eltonnunes.

  • truth I spoke too fast, if you have a while you can take a look at the second half of this video, https://www.youtube.com/watch?v=prQ0TGPFFLk&t=2s

  • No problem. I’ll see. Thanks, @Eltonnunes. Ah the part of the division you suggested does not interfere with the part where you are giving problem in the display of value.

  • your mistake is in the second couchetes, you’re passing zero, should be a

  • @Eltonnunes , thanks for the help, but the error is not at that point you pointed out. If I pass 1 instead of zero, the amount of coins will appear with a decimal place, but the amount should not have decimal place after the comma. If I do this, the output will look like this: 1 moeda(s) de R$ 1.00 
0.5 moeda(s) de R$ 1.00 
0.25 moeda(s) de R$ 0.00 
0.1 moeda(s) de R$ 2.00 
0.05 moeda(s) de R$ 0.00 
0.01 moeda(s) de R$ 3.00 
 ps.: I’m about halfway through your video.

  • vc tried the other couchete? print('{0} currency(s) of R$ {1:. 2f}'. format(round(Qtde), value))

  • @Eltonnunes, the solution you put in the video came out right. Then I saw that this last form that you commented also works. So these two shapes work (I need to better understand how this format() issue works: print('{0} moeda(s) de R$ {1:.2f}'.format(qtde, valor)) print('{} moeda(s) de R$ {:.2f}'.format(qtde, valor)) Thank you very much for the help.

  • cool that worked

Show 5 more comments

2 answers

1


The problem is that the value before the : in the formatting expression, it is not an indicator of the numerical value before the decimal places - and, yes, it is what positional argument of the format will be used - i.e., the line:

print('{0} moeda(s) de R$ {0:.2f}'.format(round(qtde), round(valor, 2)))

will print twice the first argument of the format, in this case the expression round(qtde).

In addition to arranging this, there are two things you can simplify there: (1) no need to call the round - formatting by saying the number of decimal places you want already rounds up what you need, and, (2), since Python version 3.6, it is no longer necessary to use the method .format - you can use the f-strings. With a f-string, you can place Python expressions directly inside the string, inside the keys, and before the : that specifies the formatting. Your line can look just like this:

print(f'{qtde} moeda(s) de R$ {valor:.02f}'
  • Got it, thanks a lot for the explanation, @jsbueno.

0

I got the answer via the link sent by @Eltonnunes in the comments.

The following two ways worked and answer my question:

    print('{0} moeda(s) de R$ {1:.2f}'.format(qtde, valor)) 
    print('{} moeda(s) de R$ {:.2f}'.format(qtde, valor))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.