+class Formatter {
+ private static _instance: Formatter;
+
+ private _dollar = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', 'currencySign': 'accounting' } as Intl.NumberFormatOptions);
+ private _percent = new Intl.NumberFormat('en-US', { style: 'percent', maximumFractionDigits: 3 });
+ private _decimal = new Intl.NumberFormat('en-US', { style: 'decimal', minimumFractionDigits: 5 });
+
+ private constructor() {}
+
+ static getInstance() {
+ if (!Formatter._instance) {
+ Formatter._instance = new Formatter();
+ }
+ return Formatter._instance;
+ }
+
+ dollar(value: number): string {
+ return this._dollar.format(value);
+ }
+
+ percent(value: number): string {
+ return this._percent.format(value);
+ }
+
+ decimal(value: number): string {
+ return this._decimal.format(value);
+ }
+
+ string(value: any): string {
+ return JSON.stringify(value, null, 1);
+ }
+}
+
+function formatLine(value: any, line: Line<any>): string {
+ const formatter = Formatter.getInstance();
+ const formatType = line.options.formatType;
+
+ if (typeof(value) === 'number') {
+ if (formatType == FormatType.Decimal) {
+ return formatter.decimal(value);
+ } else if (formatType == FormatType.Percent) {
+ return formatter.percent(value);
+ } else if (formatType == FormatType.Dollar || formatType == undefined) {
+ return formatter.dollar(value);
+ }
+ }
+
+ return formatter.string(value);
+}
+