The NumFmt class rounds and formats numbers according to a format specification. A NumFmt instance can be used to apply several operations according to the specified format, including:
NumFmt is used by the numeric state variables to specify how to present numbers. It provides a common and compact way of specifying numeric formatting.
The NumFmt class is a global property and does not have to be imported.
The format string specifies the formatting and rounding that will be applied to instance operations. It is set during construction and may be modified using the fmtStr instance property. It may contain the following RegExp specifications in order:
+|-|+- (optional)\d+ (optional)\.\d+ (optional)m(?<number>\d*(\.\d+)?) (optional)\.\d+ specification. If m is omitted but fraction digits are specified, the rounding multiple is 10-fraction digits.u|d (optional)c (optional)s(?<number>\d*(\.\d+)?) (optional)T.* (optional).new NumFmt(fmtStr)
new NumFmt(fmtStr, min)
new NumFmt(fmtStr, min, max)
The NumFmt constructor sets up formatting according to the fmtStr. It may also optionally set a minimum or maximum value.
fmtStrmin (optional)fmtStr. If neither - nor + is present in the fmtStr, the minimum is 0. If either is present, the minimum is the negative of the maximum value representable by the fmtStr.max (optional)fmtStr by assuming the digit '9' for each integer and fractional position.apply()apply(value)
apply(value, clampIt)
Apply a format to value. Returns a Number rounded and clamped according to the current fmtStr. If clampIt is true, the number will clamped to be between the current minimum and maximum values regardless of the clamp setting in fmtStr. If clampIt is false, the number is not clamped even if fmtStr contains c.
Non-finite values are returned as is.
valueclampIt (optional)clampIt is true, the value will be clamped to be between the current minimum and maximum values. If omitted or undefined, the value will be clamped according to the clamp setting in the fmtStr. If false, the value will not be clamped, even if the fmtStr has the clamp setting.A Number.
clamp()clamp(value)
Return value clamped to be between the current minimum and maximum values. The current fmtStr is ignored.
A Number.
fmtVal()fmtVal(value)
Return a string rounded, clamped, and formatted according to the current fmtStr. If value is not valid (see isValid(): not finite or outside the current minimum and maximum), the string 'NaN' is returned. If the fmtStr contains +, non-negative values are prefixed with +. If the fmtStr contains ,, thousands separators are included using fmtNum().
A String.
isValid()isValid(value)
Returns true if value is finite and within the current minimum and maximum values.
A Boolean.
round()round(value)
Return value rounded according to the current rndMult value. The value is not clamped. NaN is passed through unmodified.
A Number.
fmtStrGet/set the current format string. The properties that depend on the format string are recomputed if the fmtStr is set, except for min and max, which keep their current values. The limits implied by the new format are used only when min or max is subsequently set to undefined.
A string.
fDigitsThe number of fractional digits in the number specified by the fmtStr. For example, '4.2' has two fractional digits.
iDigitsThe number of integer digits in the number. For example, '4.2' has four integer digits. Defaults to 8 if the fmtStr has no integer digit count.
maxGet/set the current maximum value. If set to undefined or NaN, the current maximum value will be set to the maximum representable by fmtStr by assuming the digit '9' for each integer and fractional position. Otherwise, the value must not exceed that maximum (this is asserted) and is rounded down to a multiple of rndMult.
A Number.
minGet/set the current minimum value. If set to undefined, the current minimum value will be set to the minimum implied by fmtStr. If neither - nor + is present in the fmtStr, the minimum is 0. If either is present, the minimum is the negative of the maximum value representable by the fmtStr by assuming the digit '9' for each integer and fractional position. Otherwise, the value must not be less than that minimum (this is asserted) and is rounded up to a multiple of rndMult.
A Number.
rndMultThe rounding multiple specified by the fmtStr. Formatted values will be rounded to rndMult. It is a plain field and is recomputed when fmtStr is set.
stepThe increment/decrement step for numeric inputs. It defaults to rndMult and is overridden by the s<number> specification in the fmtStr (rounded to a multiple of rndMult).
The Thumbwheel format that was extracted from the fmtStr.
widthThe field width implied by the fmtStr. Includes a sign, integer digits, and any fraction digits with '.'. If thousand separators are requested, it includes the maximum number of separators for the number of integer digits. For example, '-4.2,' has a width of 9. It is a plain field set from the fmtStr; it should be treated as read-only.