Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 09f3f35

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
feat(formatter): Decimal Formatter (or use alias Number)
1 parent 4755dc8 commit 09f3f35

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/app/examples/grid-formatter.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class GridFormatterComponent implements OnInit {
2828
this.columnDefinitions = [
2929
{ id: 'title', name: 'Title', field: 'title', sortable: true, type: FieldType.string, width: 70 },
3030
{ id: 'phone', name: 'Phone Number using mask', field: 'phone', sortable: true, type: FieldType.number, minWidth: 100, formatter: Formatters.mask, params: { mask: '(000) 000-0000' } },
31-
{ id: 'duration', name: 'Duration (days)', field: 'duration', sortable: true, type: FieldType.number, minWidth: 90 },
31+
{ id: 'duration', name: 'Duration (days)', field: 'duration', formatter: Formatters.decimal, params: { decimalPlaces: 2 }, sortable: true, type: FieldType.number, minWidth: 90 },
3232
{ id: 'complete', name: '% Complete', field: 'percentComplete', formatter: Formatters.percentCompleteBar, type: FieldType.number, sortable: true, minWidth: 100 },
3333
{ id: 'percent2', name: '% Complete', field: 'percentComplete2', formatter: Formatters.progressBar, type: FieldType.number, sortable: true, minWidth: 100 },
3434
{ id: 'start', name: 'Start', field: 'start', formatter: Formatters.dateIso, sortable: true, type: FieldType.date, minWidth: 90, exportWithFormatter: true },
@@ -57,7 +57,7 @@ export class GridFormatterComponent implements OnInit {
5757
id: i,
5858
title: 'Task ' + i,
5959
phone: this.generatePhoneNumber(),
60-
duration: Math.round(Math.random() * 100) + '',
60+
duration: Math.random() * 100 + '',
6161
percentComplete: randomPercent,
6262
percentComplete2: randomPercent,
6363
percentCompleteNumber: randomPercent,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Column, Formatter } from './../models/index';
2+
import { decimalFormatted } from './../services/utilities';
3+
4+
export const decimalFormatter: Formatter = (row: number, cell: number, value: any, columnDef: Column, dataContext: any) => {
5+
const params = columnDef.params || {};
6+
const minDecimalPlaces = params.minDecimalPlaces || params.decimalPlaces || 2;
7+
const maxDecimalPlaces = params.maxDecimalPlaces || 2;
8+
return isNaN(+value) ? value : `${decimalFormatted(value, minDecimalPlaces, maxDecimalPlaces)}`;
9+
};
10+

src/app/modules/angular-slickgrid/formatters/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { dateTimeIsoAmPmFormatter } from './dateTimeIsoAmPmFormatter';
1212
import { dateTimeUsAmPmFormatter } from './dateTimeUsAmPmFormatter';
1313
import { dateTimeUsFormatter } from './dateTimeUsFormatter';
1414
import { dateUsFormatter } from './dateUsFormatter';
15+
import { decimalFormatter } from './decimalFormatter';
1516
import { deleteIconFormatter } from './deleteIconFormatter';
1617
import { dollarColoredBoldFormatter } from './dollarColoredBoldFormatter';
1718
import { dollarColoredFormatter } from './dollarColoredFormatter';
@@ -89,6 +90,13 @@ export const Formatters = {
8990
/** Displays a Font-Awesome delete icon (fa-trash) */
9091
deleteIcon: deleteIconFormatter,
9192

93+
/**
94+
* Display the value as x decimals formatted, defaults to 2 decimals.
95+
* You can pass "decimalPlaces" or "minDecimalPlaces" and/or "maxDecimalPlaces" to the generic "params" property, example:: `{ formatter: Formatters.decimal, params: { decimalPlaces: 3 }}`
96+
* The property "decimalPlaces" is an alias of "minDecimalPlaces"
97+
*/
98+
decimal: decimalFormatter,
99+
92100
/** Display the value as 2 decimals formatted with dollar sign '$' at the end of of the value */
93101
dollar: dollarFormatter,
94102

@@ -125,6 +133,9 @@ export const Formatters = {
125133
*/
126134
multiple: multipleFormatter,
127135

136+
/** alias of the decimal formatter */
137+
number: decimalFormatter,
138+
128139
/** Takes a cell value number (between 0.0-1.0) and displays a red (<50) or green (>=50) bar */
129140
percent: percentFormatter,
130141

0 commit comments

Comments
 (0)