NumFmt Reference

Table of contents


Introduction

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.


Format string

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:


Constructor

Syntax

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.

Parameters


Instance methods

apply()

Syntax

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.

Parameters

Return value

A Number.


clamp()

Syntax

clamp(value)

Return value clamped to be between the current minimum and maximum values. The current fmtStr is ignored.

Return value

A Number.


fmtVal()

Syntax

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

Return value

A String.


isValid()

Syntax

isValid(value)

Returns true if value is finite and within the current minimum and maximum values.

Return value

A Boolean.


round()

Syntax

round(value)

Return value rounded according to the current rndMult value. The value is not clamped. NaN is passed through unmodified.

Return value

A Number.


Instance Properties

fmtStr

Get/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.

Value

A string.


fDigits

The number of fractional digits in the number specified by the fmtStr. For example, '4.2' has two fractional digits.


iDigits

The 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.


max

Get/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.

Value

A Number.


min

Get/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.

Value

A Number.


rndMult

The 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.


step

The 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).


twFmt

The Thumbwheel format that was extracted from the fmtStr.


width

The 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.