from math import floor def get_purchase_quantity( raw_quantity:float, step_size:str ) -> float: if float( step_size ) % 1 == 0: return round( raw_quantity / float( step_size ) ) * float( step_size ) return round( raw_quantity, step_size.index( '1' ) - 1 ) def get_sale_quantity( raw_quantity:float, step_size:str ) -> float: if float( step_size ) % 1 == 0: return floor( raw_quantity / float( step_size ) ) * float( step_size ) multiplier = 10 ** ( step_size.index( '1' ) - 1 ) return floor( raw_quantity * multiplier ) / multiplier def main(): step_sizes = [ '1.00', '1.00000000', '0.10000000', '0.01000000', '0.00100000', '0.00010000', '0.00001000', '0.00000100', '10.00000000' ] raw_quantity = 38.9365461 print( f'When raw quantity equals: { raw_quantity }' ) for step_size in step_sizes: purchase_quantity = get_purchase_quantity( raw_quantity, step_size ) sale_quantity = get_sale_quantity( raw_quantity, step_size ) print( f'\n\tstep size:{step_size}, {purchase_quantity=},{sale_quantity=}\n') if __name__ == '__main__': main()