#NEWLINE# def format_number( value , max_digits , decimal_places ) : #NEWLINE# #INDENT# '\n    Formats a number into a string with the requisite number of digits and\n    decimal places.\n    ' #NEWLINE# if ( value is None ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# if isinstance ( value , decimal . Decimal ) : #NEWLINE# #INDENT# context = decimal . getcontext ( ) . copy ( ) #NEWLINE# if ( max_digits is not None ) : #NEWLINE# #INDENT# context . prec = max_digits #NEWLINE# #UNINDENT# if ( max_digits is not None ) : #NEWLINE# #INDENT# value = value . quantize ( ( decimal . Decimal ( '.1' ) ** decimal_places ) , context = context ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# context . traps [ decimal . Rounded ] = 1 #NEWLINE# value = context . create_decimal ( value ) #NEWLINE# #UNINDENT# return '{:f}' . format ( value ) #NEWLINE# #UNINDENT# if ( decimal_places is not None ) : #NEWLINE# #INDENT# return ( '%.*f' % ( decimal_places , value ) ) #NEWLINE# #UNINDENT# return '{:f}' . format ( value )