Skip to main content
@spa-tools/utilities

formatFloat()

The formatFloat() function formats a number as a float with the specified number of decimal places optional grouping with additional option of how to handle negative numbers.

Usage

import { formatFloat } from '@spa-tools/utilities';

const formatted1 = formatFloat(1234.5678, 2, false);
console.log(formatted1);
// --> 1234.57

const formatted2 = formatFloat(90123456.78951, 3, true);
console.log(formatted2);
// --> 90,123,456.790

const formatted3 = formatFloat(-123456789.123456, 4, true, true);
console.log(formatted3);
// --> (123,456,789.1235)

Arguments

NameTypeRequired?DefaultDescription
valuenumberyes-The value to format
decimalPlacesnumberno2The number of decimal places to round to
useGroupingbooleannotrueDetermines whether or not to add grouping (,)s to result
parentheticalNegativesbooleannofalseDetermines whether or not to use parentheses for negative numbers instead of - sign

Returns

Returns formatted float.