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

NumberFormat(num, inputDecimal)

getOriginal

Returns the number as it was passed in, which may include non-number characters. Added in v1.0.2

getOriginal()

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)

setCurrency

Sets a switch that indicates if the number should be displayed as currency

setCurrency(isC)

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)

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)

setCurrencyValue

Sets the symbol for currency.

setCurrencyValue(val)

setInputDecimal

Set the decimal value for the input. New in v1.5.0

setInputDecimal(val)

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)

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)

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)

setPlaces

Sets the precision of decimal places. Modified in v1.5.1 and v1.5.4

setPlaces(p, tr)

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)

toFormatted

Returns the number as a string formatted according to the settings. Modified in v1.5.0 and v1.5.1

toFormatted()

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()

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()

About this page: