Write a Python function to round floating numbers half up to the specified precision…
- User
Write a Python function to round floating numbers half up to the specified precision using the decimal module.
- Assistant
Sure, here is a Python function that does that:
pythonfrom decimal import Decimal, ROUND_HALF_UP def round_half_up(number, precision): return Decimal(str(number)).quantize(Decimal('0.' + '0' * precision), rounding=ROUND_HALF_UP) # Usage example: print(round_half_up(3.14159, 2)) # Outputs: 3.14 print(round_half_up(3.14559, 2)) # Outputs: 3.15
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.