v1.2.0.1-beta1

Description

A few convenience methods to extend the standard Number prototype.

Public Member Functions

number round (number precision)
 
number clamp (number min, number max)
 
number constrain (number min, number max)
 
string format (format)
 
Date fromLocaleString (locale, numberString)
 
string toLocaleString (locale, format, precision)
 
string toLocaleCurrencyString (locale, symbol)
 

Member Function Documentation

◆ round()

number round ( number  precision)

Round value to precision decimal places.

◆ clamp()

number clamp ( number  min,
number  max 
)

Constrain value to between min and max values.

◆ constrain()

number constrain ( number  min,
number  max 
)

Alias for Number.clamp()

◆ format()

string format (   format)

Formats this number according the specified .NET-style format string.

This function emulates the .NET numeric types' *.ToString() methods (eg. Int32.ToString()). For example:

console.log((1234.5678).format("F2")); // Prints: "1234.56"
string format(format)
Formats this number according the specified .NET-style format string.

See formatting string reference at https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#standard-format-specifiers

See also
String.format(), toLocaleString(), toLocaleCurrencyString()

◆ fromLocaleString()

Date fromLocaleString (   locale,
  numberString 
)

Returns a Number by parsing numberString using the conventions of the supplied locale.

Parameters
localeA Locale object, obtained with the global locale() method. If not specified, the default locale() will be used.
numberStringThe input string to parse.

For example, using the German locale:

var german = locale("de_DE");
var d;
d = Number.fromLocaleString(german, "1234,56") // d == 1234.56
d = Number.fromLocaleString(german, "1.234,56") // d == 1234.56
d = Number.fromLocaleString(german, "1234.56") // throws exception
d = Number.fromLocaleString(german, "1.234") // d == 1234.0
A few convenience methods to extend the standard Number prototype.
Date fromLocaleString(locale, numberString)
Returns a Number by parsing numberString using the conventions of the supplied locale.
See also
locale(), Locale

◆ toLocaleString()

string toLocaleString (   locale,
  format,
  precision 
)

Converts the Number to a string suitable for the specified locale in the specified format, with the specified precision.

Parameters
localeA Locale object, obtained with the global locale() method. If not specified, the default locale() will be used.
formatOne of the format specifier strings shown below. Default is ‘'f’`
precisionNumber of decimal places to show. Default is 2 decimals.

Valid formats are:

'f' Decimal floating point, e.g. 248.65
'e' Scientific notation using e character, e.g. 2.4865e+2
'E' Scientific notation using E character, e.g. 2.4865E+2
'g' Use the shorter of e or f
'G' Use the shorter of E or f

The following example shows a number formatted for the German locale:

console.log(Number(4742378.423).toLocaleString(locale("de_DE"));
// 4.742.378,423
string toLocaleString(locale, format, precision)
Converts the Number to a string suitable for the specified locale in the specified format,...

You can apply toLocaleString() directly to constants, provided the decimal is included in the constant, e.g.

123.0.toLocaleString(locale("de_DE")) // OK
123..toLocaleString(locale("de_DE")) // OK
123.toLocaleString(locale("de_DE")) // fails
See also
locale(), Locale

◆ toLocaleCurrencyString()

string toLocaleCurrencyString (   locale,
  symbol 
)

Converts the Number to a currency using the currency and conventions of the specified locale.

If symbol is specified it will be used as the currency symbol.

console.log(Number(2378.45).toLocaleCurrencyString(locale("de_DE")));
// 2.378,45€
console.log(Number(2378.45).toLocaleCurrencyString(locale("de_DE", "\u2133")));
// 2.378,45ℳ
string toLocaleCurrencyString(locale, symbol)
Converts the Number to a currency using the currency and conventions of the specified locale.
See also
locale(), Locale