0

I have a random number generated, then I use round to get it to 2 decimal places.

Occasionally I get an integer such as "2" i would like this to be displayed as "2.00"

Or when I get "3.1" to be displayed as "3.10"

I have tried rounding to 2 d.p using round()

import random

#this creates a number between 2 and 5.99 by adding a decimal to an integer then rounds the sum to 2 d.p

def second_question():
    temp_var_4 = random.randint(2,5) + round(random.random(),2)
    print(temp_var_4)
second_question()

No error messages just returning some numbers to integers or to one d.p

James Novis
  • 343
  • 5
  • 15
  • So you want to print 2 as 2.00 and 3 as 3.10 right @JamesNovis? I have linked a duplicate for the same, let me know if it doesn't solve your problem – Devesh Kumar Singh Jul 08 '19 at 07:28
  • `print( "{:.2f}".format(3.1) )`, `print( "{:.2f}".format(2) )` See more on [PyFormat.info](https://pyformat.info/) – furas Jul 08 '19 at 07:31

1 Answers1

1

You can control the output formatting explicitly by using a format string:

print('%.2f' % temp_var_4)
Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22