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.

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.

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.

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.


max

Get/set the current maximum value. If set to undefined, the current maximum value will be set to the maximum representable by fmtStr by assuming the digit '9' for each integer and fractional position.

Value

A string.


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 - is not present in the fmtStr, the minimum is 0. If - is present in the fmtStr, the minimum is the negative of the maximum value representable by the fmtStr by assuming the digit '9' for each integer and fractional position.

Value

A string.


rndMult

The rounding multiple and increment/decrement step specified by the fmtStr. Formatted values will be rounded to rndMult. Read-only.


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. Read-only.