-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {RoundPipe, RoundType} from './round.pipe'; | ||
|
||
describe('RoundPipe', () => { | ||
let pipe: RoundPipe; | ||
|
||
beforeEach(() => { | ||
pipe = new RoundPipe(); | ||
}); | ||
|
||
it('should create an instance', () => { | ||
expect(pipe).toBeTruthy(); | ||
}); | ||
|
||
it(`should round off positive number`, () => { | ||
expect(pipe.transform(1234.56)).toEqual(1235); | ||
expect(pipe.transform(1234.456, undefined, RoundType.Default)).toEqual(1234); | ||
expect(pipe.transform(1234.56789, 3)).toEqual(1234.568); | ||
expect(pipe.transform(9876.54321, 3)).toEqual(9876.543); | ||
}); | ||
|
||
it(`should round off of negative number`, () => { | ||
expect(pipe.transform(-1234.56)).toEqual(-1235); | ||
expect(pipe.transform(-1234.456, undefined, RoundType.Default)).toEqual(-1234); | ||
expect(pipe.transform(-1234.56789, 3)).toEqual(-1234.568); | ||
expect(pipe.transform(-9876.54321, 3)).toEqual(-9876.543); | ||
}); | ||
|
||
it(`should round down positive number`, () => { | ||
expect(pipe.transform(1234.56, undefined, RoundType.Floor)).toEqual(1234); | ||
expect(pipe.transform(1234.56789, 3, RoundType.Floor)).toEqual(1234.567); | ||
expect(pipe.transform(9876.54321, 3, RoundType.Floor)).toEqual(9876.543); | ||
}); | ||
|
||
it(`should round down negative number`, () => { | ||
expect(pipe.transform(-1234.56, undefined, RoundType.Floor)).toEqual(-1235); | ||
expect(pipe.transform(-1234.56789, 3, RoundType.Floor)).toEqual(-1234.568); | ||
expect(pipe.transform(-9876.54321, 3, RoundType.Floor)).toEqual(-9876.544); | ||
}); | ||
|
||
it(`should round up positive number`, () => { | ||
expect(pipe.transform(1234.56, undefined, RoundType.Ceil)).toEqual(1235); | ||
expect(pipe.transform(1234.56789, 3, RoundType.Ceil)).toEqual(1234.568); | ||
expect(pipe.transform(9876.54321, 3, RoundType.Ceil)).toEqual(9876.544); | ||
}); | ||
|
||
it(`should round up negative number`, () => { | ||
expect(pipe.transform(-1234.56, undefined, RoundType.Ceil)).toEqual(-1234); | ||
expect(pipe.transform(-1234.56789, 3, RoundType.Ceil)).toEqual(-1234.567); | ||
expect(pipe.transform(-9876.54321, 3, RoundType.Ceil)).toEqual(-9876.543); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Pipe, PipeTransform } from '@angular/core'; | ||
|
||
export enum RoundType { | ||
Default, | ||
Floor, | ||
Ceil | ||
} | ||
|
||
@Pipe({ | ||
name: 'round' | ||
}) | ||
export class RoundPipe implements PipeTransform { | ||
|
||
transform(value: number, decimalPlaces: number = 0, roundType?: RoundType): number { | ||
const factor = Math.pow(10, decimalPlaces); | ||
|
||
switch (roundType) { | ||
case RoundType.Floor: | ||
return Math.floor(value * factor) / factor; | ||
case RoundType.Ceil: | ||
return Math.ceil(value * factor) / factor; | ||
case RoundType.Default: | ||
default: | ||
return Math.round(value * factor) / factor; | ||
} | ||
} | ||
|
||
} |