Class that implements formatting and parsing of numbers and Dates.
By default, Globalize uses the American English culture. To switch cultures, include the appropriate **wijmo.culture** file after the wijmo files.
The example below shows how you can use the Globalize class to format dates, times, and numbers in different cultures:
{@sample Core/Globalization/Formatting Example}
format(value: any, format: string, trim?: boolean, truncate?: boolean, defaultPrec?: number): string
Formats a number or a date.
The format strings used with the format function are similar to the ones used by the .NET Globalization library. The tables below contains links that describe the formats available:
Number or Date to format (all other types are converted to strings).
Format string to use when formatting numbers or dates.
Whether to remove trailing zeros from numeric results.
Whether to truncate the numeric values rather than round them.
Precision to use if not specified in the format string.
formatDate(value: Date, format: string): string
Formats a date using the current culture.
The format parameter contains a .NET-style Date format string with the following additions:
For example:
import { Globalize } from '@mescius/wijmo';
let dt = new Date(2015, 9, 1); // Oct 1, 2015
console.log('result', Globalize.format(dt, '"FY"EEEE"Q"U') + ' (US culture)');
**result** FY2016Q1 (US culture)
Another addition is available for dealing with complex eras such as those defined in the Japanese culture:
{@sample Core/Globalization/Formatting/purejs Example}
formatNumber(value: number, format: string, trim?: boolean, truncate?: boolean, defaultPrec?: number): string
Formats a number using the current culture.
The formatNumber method accepts all .NET-style Standard Numeric Format Strings and provides support for scaling, prefixes, suffixes, and custom currency symbols.
Numeric format strings take the form Axxsscc, where:
The following table describes the standard numeric format specifiers and displays sample output produced by each format specifier for the default culture.
c Currency: formatNumber(1234, 'c') => '$1,234.00'
d Decimal (integers): formatNumber(-1234, 'd6') => '-001234'
e Scientific Notation (lower-case 'e'): formatNumber(123.456, 'e6') => '1.234560e+2'
E Scientific Notation (upper-case 'e'): formatNumber(123.456, 'E6') => '1.234560E+2'
f Fixed-point: formatNumber(1234.5, 'f2') => '1234.50'
F Fixed-point (with thousand separators): formatNumber(1234.5, 'F2') => '1,234.50'
g General (no trailing zeros): formatNumber(1234.50, 'g2') => '1234.5'
G General (no trailing zeros, thousand separators): formatNumber(1234.5, 'G2') => '1,234.5'
n Number: formatNumber(1234.5, 'n2') => '1,234.50'
p Percent: formatNumber(0.1234, 'p2') => '12.34%'
P Percent (no thousand separators): formatNumber(12.34, 'P2') => '1234%'
r Round-trip (same as g15): formatNumber(0.1234, 'r') => '0.1234'
x Hexadecimal (integers): formatNumber(1234, 'x6') => '0004d2'
The scaling specifier is especially useful when charting large values. For example, the markup below creates a chart that plots population versus GDP. The raw data expresses the population is units and the GDP in millions. The scaling specified in the axes formats causes the chart to show population in millions and GDP in trillions:
import { FlexChart} from '@mescius/wijmo.chart';
new FlexChart('#theChart', {
itemsSource: countriesGDP,
bindingX: 'pop',
chartType: 'Scatter',
series: [
{ name: 'GDP', binding: 'gdp' }
],
axisX: {
title: 'Population (millions)'
format: 'n0,,'
},
axisY: {
title: 'GDP (US$ trillions)'
format: 'c0,,'
}
});
The format string may also include constant prefix and suffix strings to be added to the output. If present, the prefix and suffix are specified as *double-quoted* strings at the start and end of the format string:
import { Globalize } from '@mescius/wijmo';
console.log(Globalize.formatNumber(value, '"thousands: "c3," k"'));
console.log(Globalize.formatNumber(value, '"millions: "c1,," M"'));
Number to format.
.NET-style standard numeric format string (e.g. 'n2', 'c4', 'p0', 'g2', 'd2').
Whether to remove trailing zeros from the result.
Whether to truncate the value rather than round it.
Precision to use if not specified in the format string.
getFirstDayOfWeek(): number
Gets the first day of the week according to the current culture.
The value returned is between zero (Sunday) and six (Saturday).
getNumberDecimalSeparator(): string
Gets the symbol used as a decimal separator in numbers.
parseDate(value: string, format: string, refDate?: Date): Date
Parses a string into a Date.
Two-digit years are converted to full years based on the value of the calendar's **twoDigitYearMax** property. By default, this is set to 2029, meaning two-digit values of 30 to 99 are parsed as 19xx, and values from zero to 29 are parsed as 20xx.
You can change this threshold by assigning a new value to the calendar. For example:
// get calendar
var cal = wijmo.culture.Globalize.calendar;
// default threshold is 2029, so "30" is parsed as 1930
cal.twoDigitYearMax = 2029;
var d1 = wijmo.Globalize.parseDate('30/12', 'yy/MM'); // dec 1930
// changing threshold to 2100, so all values are parsed as 20**
cal.twoDigitYearMax = 2100;
var d2 = wijmo.Globalize.parseDate('30/12', 'yy/MM'); // dec 2030
String to convert to a Date.
Format string used to parse the date.
Date to use as a reference in case date or time parts are not specified in the format string (e.g. format = 'MM/dd').
parseFloat(value: string, format?: string): number
Parses a string into a floating point number.
String to convert to a number.
Format to use when parsing the number.
parseInt(value: string, format?: string): number
Parses a string into an integer.
String to convert to an integer.
Format to use when parsing the number.