1

So say I have something that looks like this:

def probability_color():
  if color == 'red':
    return float(3/10)

so essentially it should return .30 instead of anything longer. My specific problem includes fractions that aren't as clean as this example so I'm getting very long decimal float values in this particular scenario. Is there a simple solution that would format it rather than using something like round()?

CodingNoob
  • 59
  • 4

1 Answers1

0

Just use the round format in Python to help. Here is the improved code:

def probability_color():
  if color == 'red':
    # Decimal places :)
    return float('%.2f' % (1/10))
xnarf
  • 100
  • 9