Convert to Decimal
Rounds a number to a specified number of decimal places and returns it as a number or string.
convertToDecimal
The convertToDecimal function rounds a number to a specified number of decimal places and returns it as either a number or string, based on the provided options.
Function Signature
function convertToDecimal<T extends boolean>(input: Numeric, options?: DecimalOptions<T>): ConvertedDecimal<T>Parameters
input(Numeric): A number or a numeric string that needs to be rounded.options(DecimalOptions<T>, optional): Configuration object to customize the rounding behavior.decimalPlaces(optional): The number of decimal places to round to. Defaults to2.isString(optional): If set totrue, the result will be returned as a string. Defaults tofalse, which returns the result as a number.
Return Value
- Returns the input number rounded to the specified decimal places.
- The type of the return value depends on the
isStringoption:- If
isStringisfalse(default), the result is returned as anumber. - If
isStringistrue, the result is returned as astring.
- If
Example Usage
playground.ts
Related Functions
roundNumber: Use when you need rounding to specific decimal places (including negative places for whole number rounding) without string output options.roundToNearest: Use when you need to round to arbitrary intervals (like multiples of 5, 10, etc.) rather than decimal places.- For a comparison, see
roundNumber: Comparison with Similar Functions.
Aliases
convertToFixed
Last updated: Tue, Jun 16, 2026 08:16:20PM (UTC)
