Q:

How do I represent the return of a specific probability fraction to be shown as a float but limited to two decimal places?

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()?

A:

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))

