Number

Myna extensions to the JavaScript Number object

Summary
NumberMyna extensions to the JavaScript Number object
Functions
timesExecutes the supplied function parseInt(this) times.
formatFormats this number as a string

Functions

times

Number.prototype.times = function(func)

Executes the supplied function parseInt(this) times.

Parameters

funca function to execute.  This function will be called with the current 0-based index

Example

//extra dot forces 5 to be a number object
5..times(function(i){
$res.print(i + "<br>")
});

format

Number.prototype.format = function(formatString)

Formats this number as a string

Parameters

formatStringString pattern for output.

About format strings

  • The number of digits after the decimal specifies the number of decimal places in the resulting string.
  • The presence of a thousand separator in the format string specifies that a thousand separator is inserted separating thousand groups.
  • Any other characters will will be copied to output

Examples (123456.789)

123456.789.format("0")          - (123457) show only digits, no precision, rounding
123456.789.format("0.00") - (123456.79) show only digits, 2 precision, rounding
123456.789.format("0.0000") - (123456.7890) show only digits, 4 precision
123456.789.format("$0,000") - ($123,457) show comma and digits, no precision, rounding
123456.789.format("0,000.00") - (123,456.79) show comma and digits, 2 precision, rounding
123456.789.format("0,0.00") - (123,456.79) shortcut method, show comma and digits, 2 precision, rounding

Adapted from Sencha ExtJS v4 Ext.util.Format.number()

Number.prototype.times = function(func)
Executes the supplied function parseInt(this) times.
Number.prototype.format = function(formatString)
Formats this number as a string