round
Perform advanced rounding operations using multiple methods including round to nearest, floor, ceil, and truncate. Specify decimal places and handle single values or lists for mathematical calculations.
Instructions
Advanced rounding operations with multiple methods.
Methods: - round: Round to nearest (3.145 → 3.15 at 2dp) - floor: Always round down (3.149 → 3.14) - ceil: Always round up (3.141 → 3.15) - trunc: Truncate towards zero (-3.7 → -3, 3.7 → 3)
Examples:
ROUND TO NEAREST: values=3.14159, method="round", decimals=2 Result: 3.14
FLOOR (DOWN): values=3.14159, method="floor", decimals=2 Result: 3.14
CEIL (UP): values=3.14159, method="ceil", decimals=2 Result: 3.15
MULTIPLE VALUES: values=[3.14159, 2.71828], method="round", decimals=2 Result: [3.14, 2.72]
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context | No | Optional annotation to label this calculation (e.g., 'Bond A PV', 'Q2 revenue'). Appears in results for easy identification. | |
| output_mode | No | Output format: full (default), compact, minimal, value, or final. See batch_execute tool for details. | full |
| values | Yes | Single value or list (e.g., 3.14159 or [3.14, 2.71]) | |
| method | No | Rounding method | round |
| decimals | No | Number of decimal places |
Implementation Reference
- src/vibe_math_mcp/tools/basic.py:147-179 (registration)Registers the 'round' MCP tool with detailed description, input schema (values: Union[float, List[float]], method: Literal['round','floor','ceil','trunc'], decimals: int), and annotations.@mcp.tool( name="round", description="""Advanced rounding operations with multiple methods. Methods: - round: Round to nearest (3.145 → 3.15 at 2dp) - floor: Always round down (3.149 → 3.14) - ceil: Always round up (3.141 → 3.15) - trunc: Truncate towards zero (-3.7 → -3, 3.7 → 3) Examples: ROUND TO NEAREST: values=3.14159, method="round", decimals=2 Result: 3.14 FLOOR (DOWN): values=3.14159, method="floor", decimals=2 Result: 3.14 CEIL (UP): values=3.14159, method="ceil", decimals=2 Result: 3.15 MULTIPLE VALUES: values=[3.14159, 2.71828], method="round", decimals=2 Result: [3.14, 2.72]""", annotations=ToolAnnotations( title="Advanced Rounding", readOnlyHint=True, idempotentHint=True, ), )
- src/vibe_math_mcp/tools/basic.py:180-213 (handler)Executes the rounding logic: handles single value or list, uses NumPy functions (np.round, np.floor, np.ceil, np.trunc) scaled by 10**decimals, formats result with format_result.async def round_values( values: Annotated[ Union[float, List[float]], Field(description="Single value or list (e.g., 3.14159 or [3.14, 2.71])"), ], method: Annotated[ Literal["round", "floor", "ceil", "trunc"], Field(description="Rounding method") ] = "round", decimals: Annotated[int, Field(description="Number of decimal places", ge=0)] = 0, ) -> str: """Advanced rounding operations.""" try: is_single = isinstance(values, (int, float)) vals = [values] if is_single else values arr = np.array(vals, dtype=float) if method == "round": result = np.round(arr, decimals) elif method == "floor": result = np.floor(arr * 10**decimals) / 10**decimals elif method == "ceil": result = np.ceil(arr * 10**decimals) / 10**decimals elif method == "trunc": result = np.trunc(arr * 10**decimals) / 10**decimals else: raise ValueError(f"Unknown method: {method}") final_result = float(result[0]) if is_single else result.tolist() return format_result(final_result, {"method": method, "decimals": decimals}) except Exception as e: raise ValueError(f"Rounding operation failed: {str(e)}")