JavaScript Number Format v1.5.4 - API
A custom JavaScript object to format numbers
Includes currency, commas, and decimal precision
This page references NumberFormat, a custom script available for download
- The constructor
- getOriginal
- setCommas
- setCurrency
- setCurrencyPosition
- setCurrencyPrefix
- setCurrencyValue
- setInputDecimal
- setNegativeFormat
- setNegativeRed
- setNumber
- setPlaces
- setSeparators
- toFormatted
- toPercentage
- toUnformatted
The constructor
NumberFormat(num, inputDecimal)
-
num - The number to be formatted.
Also refer to setNumber -
inputDecimal - (Optional) The decimal character for the input
Also refer to setInputDecimal
getOriginal
Returns the number as it was passed in, which may include non-number characters. Added in v1.0.2
getOriginal()
- No arguments
setCommas
Sets a switch that indicates if there should be commas. The separator value is set to a comma and the decimal value is set to a period. Modified in v1.5.0
setCommas(isC)
- isC - true: the number should be formatted with commas; false: no commas
setCurrency
Sets a switch that indicates if the number should be displayed as currency
setCurrency(isC)
- isC - true: display currency; false: don't display currency
setCurrencyPosition
Sets the position for currency, which includes position relative to the numbers and negative sign.
This method does not automatically put the negative sign at the left or right.
They are left by default, and would need to be set right with setNegativeFormat.
New in v1.5.0
LEFT_OUTSIDE example: $-1.00
LEFT_INSIDE example: -$1.00
RIGHT_INSIDE example: 1.00$-
RIGHT_OUTSIDE example: 1.00-$
setCurrencyPosition(cp)
- cp - The position. Use one of the constants.
setCurrencyPrefix
Sets the symbol for currency. The symbol will show up on the left of the numbers and outside a negative sign. Modified in v1.5.0 so it now calls setCurrencyValue and setCurrencyPosition(this.LEFT_OUTSIDE)
setCurrencyPrefix(cp)
- cp - The symbol
setCurrencyValue
Sets the symbol for currency.
setCurrencyValue(val)
- val - The symbol. e.g. '$'
setInputDecimal
Set the decimal value for the input. New in v1.5.0
setInputDecimal(val)
- val - The decimal value for the input.
setNegativeFormat
Set the format for negative numbers. New in v1.5.0
LEFT_DASH example: -1000
RIGHT_DASH example: 1000-
PARENTHESIS example: (1000)
setNegativeFormat(format)
- format - The format. Use one of the constants.
setNegativeRed
Format the number red if it's negative. For use in HTML markup, not in text boxes. New in v1.5.0
setNegativeRed(isRed)
- isRed - true: to format the number red if negative, black if positive; false: for it to always be black font.
setNumber
Sets the number. If there is a non-period decimal format for the input, setInputDecimal should be called before calling setNumber. Modified in v1.5.0
setNumber(num, inputDecimal)
- num - The number to be formatted
-
inputDecimal - (Optional) The decimal character for the input
Also refer to setInputDecimal
setPlaces
Sets the precision of decimal places. Modified in v1.5.1 and v1.5.4
setPlaces(p, tr)
- p - The number of places. The constant NO_ROUNDING (which equals -1) turns off rounding to a set number of places. Any other number of places less than or equal to zero is considered zero.
- tr - Whether to truncate instead of rounding. (optional) Values can be true or false, or if left blank will count as false.
setSeparators
One purpose of this method is to set a switch that indicates if there should be separators between groups of numbers. Also, can use it to set the values for the separator and decimal. For example, in the value 1,000.00 the comma (,) is the separator and the period (.) is the decimal. Both separator and decimal are optional. The separator and decimal cannot be the same value. If they are, decimal with be changed. New in v1.5.0
Can use the following constants (via the instantiated object) for separator or decimal:
COMMA
PERIOD
setSeparators(isC, separator, decimal)
- isC - true, if there should be separators; false, if there should be no separators
- separator - the value of the separator (optional).
- decimal - the value of the decimal (optional).
toFormatted
Returns the number as a string formatted according to the settings. Modified in v1.5.0 and v1.5.1
toFormatted()
- No arguments
toPercentage
Format the current number as a percentage. This is separate from most of the regular formatting settings. The exception is the number of decimal places. If a number is 0.123 it will be formatted as 12.3%
toPercentage()
- No arguments
toUnformatted
toUnformatted - Returns the number as just a number. If the original value was '100,000', then this method will return the number 100000 Modified comments in v1.0.2, because this method no longer returns the original value. Refer to getOriginal instead.
toUnformatted()
- No arguments