vanillajs-helpers

Collection of convenience code snippets (helpers) that aims to make it a little easier to work with vanilla JS

View the Project on GitHub Tokimon/vanillajs-helpers

vanillajs-helpers > “limitDecimals”

External module: “limitDecimals”

Index

Functions


Functions

limitDecimals

limitDecimals(num: numberstring, decimals?: numberstring): string

Defined in limitDecimals.ts:33

Limit decimals of a floating number to specified length. The length depends on decimals which can have the following settings (n = integer):

Char

Description

>n

Minimum number of decimals, if the current number of decimals are shorter than the defined length, extra 0 (zeros) will be added.

<n

Maximum number of decimals, longer decimals will be rounded and shortened down to this number.

n

Match this exact number of decimals, rounding longer decimals and adding extra 0 (zeroes) to shorter ones.

// Exact number of decimals
limitDecimals(123.4567) // -> 123.46
limitDecimals(123, 5) // -> 123.00000

// Max number of decimals
limitDecimals(123.4567, '<3') // -> 123.457
limitDecimals(123, '<3') // -> 123

// Min number decimals
limitDecimals(123.4, '>4') // -> 123.4000
limitDecimals(123.456789, '>4') // -> 123.456789

Parameters:

Param Type Default value Description
num numberstring - Number to limit the decimals on
decimals numberstring 2 Setting for how to handle the decimals

Returns: string