HexagonJS
Edit Page
Format
Various number formatting functions.
Examples
hx.round(9.9999, { sf: 3 })        // 10
hx.round(1.1111, { sf: 3 })        // 1.11
hx.round(1.5555, { sf: 3 })        // 1.56
hx.round(10.5555, { sf: 3 })       // 10.6
hx.round(100.5555, { sf: 3 })      // 101
hx.round(1000.5555, { sf: 3 })     // 1000
hx.round(1000000.5555, { sf: 3 })  // 1000000
hx.round(1200000.5555, { sf: 3 })  // 1200000

hx.si(9.9999, { sf: 3 })           // 10
hx.si(1.1111, { sf: 3 })           // 1.11
hx.si(1.5555, { sf: 3 })           // 1.56
hx.si(10.5555, { sf: 3 })          // 10.6
hx.si(100.5555, { sf: 3 })         // 101
hx.si(1000.5555, { sf: 3 })        // 1K
hx.si(1000000.5555, { sf: 3 })     // 1M
hx.si(1200000.5555, { sf: 3 })     // 1.2M

hx.exp(9.9999, { sf: 3 })          // 10e+0
hx.exp(1.1111, { sf: 3 })          // 1.11e+0
hx.exp(1.5555, { sf: 3 })          // 1.56e+0
hx.exp(10.5555, { sf: 3 })         // 1.06e+1
hx.exp(100.5555, { sf: 3 })        // 1.01e+2
hx.exp(1000.5555, { sf: 3 })       // 1e+3
hx.exp(1000000.5555, { sf: 3 })    // 1e+6
hx.exp(1200000.5555, { sf: 3 })    // 1.2e+6

hx.fixed(9.9999, { digits: 3 })        // 10.000
hx.fixed(1.1111, { digits: 3 })        // 1.111
hx.fixed(1.5555, { digits: 3 })        // 1.556
hx.fixed(10.5555, { digits: 3 })       // 10.556
hx.fixed(100.5555, { digits: 3 })      // 100.556
hx.fixed(1000.5555, { digits: 3 })     // 1000.556
hx.fixed(1000000.5555, { digits: 3 })  // 1000000.556
hx.fixed(1200000.5555, { digits: 3 })  // 1200000.556

hx.zeroPad(1)  // 01
hx.zeroPad(12)  // 12
hx.zeroPad(1, { length: 10 }) // 0000000001
Api
Objects
hx.formatdeprecated
Deprecated
The format factories have been deprecated in favour of functions that take a number directly and return the formatted number.
An object containing a set of functions to create number formatting functions.
Functions
expsfNumberstrictBooleanFunctiondeprecated
Deprecated
The format factories have been deprecated in favour of functions that take a number directly and return the formatted number.
A function to format a number in E notation form
hx.format.exp(2)(111111) // Returns "1.1e5"
Arguments
The number of significant figures to use.
strictBoolean
Whether to only allow numbers or allow strings to be passed in (if a string is passed in, it will be returned unmodified if strict is false)
Default: false
Returns
functionnumberNumberString
Arguments
numberNumber
The number to format.
Returns
The formatted string
fixeddigitsNumberstrictBooleanFunctiondeprecated
Deprecated
The format factories have been deprecated in favour of functions that take a number directly and return the formatted number.
A function to format a number to a specific number of decimal places.
hx.format.fixed(3)(999) // Returns "999.000"
hx.format.fixed(3)(12.123123) // Returns "12.123"
Arguments
digitsNumber
The number of digits to limit the number to.
strictBoolean
Whether to only allow numbers or allow strings to be passed in (if a string is passed in, it will be returned unmodified if strict is false)
Default: false
Returns
functionnumberNumberString
Arguments
numberNumber
The number to format.
Returns
The formatted string
roundsfNumberstrictBooleanFunctiondeprecated
Deprecated
The format factories have been deprecated in favour of functions that take a number directly and return the formatted number.
A function to round a number to a specific number of significant figures.
hx.format.round(4)(3.1415926) // Returns "3.142"
Arguments
The number of significant figures to use.
strictBoolean
Whether to only allow numbers or allow strings to be passed in (if a string is passed in, it will be returned unmodified if strict is false)
Default: false
Returns
functionnumberNumberString
Arguments
numberNumber
The number to format.
Returns
The formatted string
sisfNumberstrictBooleanFunctiondeprecated
Deprecated
The format factories have been deprecated in favour of functions that take a number directly and return the formatted number.
A function to format a number with a SI prefix or unit prefix
hx.format.si(1)(10000) // Returns "10K"
Arguments
The number of significant figures to use.
strictBoolean
Whether to only allow numbers or allow strings to be passed in (if a string is passed in, it will be returned unmodified if strict is false)
Default: false
Returns
functionnumberNumberString
Arguments
numberNumber
The number to format.
Returns
The formatted string
zeroPadlengthNumberstrictBooleanFunctiondeprecated
Deprecated
The format factories have been deprecated in favour of functions that take a number directly and return the formatted number.
A function to pad numbers to return a string of zeros followed by the passed in number.
hx.format.zeroPad(2)(1)   // returns "01"
hx.format.zeroPad(10)(100)  // returns "0000000100"
Arguments
lengthNumber
The length of the output string
strictBoolean
Whether to only allow numbers or allow strings to be passed in (if a string is passed in, it will be returned unmodified if strict is false)
Default: false
Returns
functionnumberNumberString
Arguments
numberNumber
The number to format.
Returns
The formatted string
Functions
hx.expnumberNumberoptionsObjectString
A function to format a number in E notation form
hx.exp(111111) // Returns "1.1e5"
Arguments
numberNumber
The number to format.
options
Properties
The number of significant figures to use.
Default: 2
strictBoolean
Whether to only allow numbers or allow strings to be passed in (if a string is passed in, it will be returned unmodified if strict is false)
Default: false
Returns
The formatted string
hx.fixednumberNumberoptionsObjectString
A function to format a number to a specific number of decimal places.
hx.fixed(999) // Returns "999.00"
hx.fixed(12.123123, { digits: 3 }) // Returns "12.123"
Arguments
numberNumber
The number to format.
options
Properties
digitsNumber
The number of digits to limit the number to.
Default: 2
strictBoolean
Whether to only allow numbers or allow strings to be passed in (if a string is passed in, it will be returned unmodified if strict is false)
Default: false
Returns
The formatted string
hx.roundnumberNumberoptionsObjectString
A function to round a number to a specific number of significant figures.
hx.round(3.1415926,) // Returns "3.1"
hx.round(3.1415926, { sf: 4 }) // Returns "3.142"
Arguments
numberNumber
The number to format.
options
Properties
The number of significant figures to use.
Default: 2
strictBoolean
Whether to only allow numbers or allow strings to be passed in (if a string is passed in, it will be returned unmodified if strict is false)
Default: false
Returns
The formatted string
hx.sinumberNumberoptionsObjectString
A function to format a number with a SI prefix or unit prefix
hx.si(11000) // Returns "11K"
hx.si(11000, { sf: 1 }) // Returns "10K"
Arguments
numberNumber
The number to format.
options
Properties
The number of significant figures to use.
Default: 2
strictBoolean
Whether to only allow numbers or allow strings to be passed in (if a string is passed in, it will be returned unmodified if strict is false)
Default: false
Returns
The formatted string
hx.zeroPadnumberNumberoptionsObjectString
A function to pad numbers to return a string of zeros followed by the passed in number.
hx.zeroPad(1) // returns "01"
hx.zeroPad(100, { length: 10 }) // returns "0000000100"
Arguments
numberNumber
The number to format.
options
Properties
lengthNumber
The length of the output string
Default: 2
strictBoolean
Whether to only allow numbers or allow strings to be passed in (if a string is passed in, it will be returned unmodified if strict is false)
Default: false
Returns
The formatted string