Skip to content

Commit

Permalink
enhancement Created round pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
kneurgao committed Aug 1, 2020
1 parent 9dec203 commit 23ad7af
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class YourComponent {
- [min](#min)
- [pct](#pct)
- [pow](#pow)
- [round](#round)
- [sqrt](#sqrt)
- [sum](#sum)
- [Generic Pipes](#generic-pipes)
Expand Down Expand Up @@ -375,6 +376,31 @@ Usage: `base | pow [:exponent]`
```


### round

Returns the rounded value of given number. By default the value is rounded to the nearest integer.

It also accepts an optional argument `RoundType` for rounding the value up or down.\
`RoundType.Default` = Default rounding as in `Math.round()`
`RoundType.Floor` = Round down as in `Math.floor()`
`RoundType.Ceil` = Round up as in `Math.ceil()`

Optionally, the number of decimal places to which the result should be rounded may also be specified.

Usage: `number | round [:decimalPlaces][:roundType]`

```html
{{ 1234.56789 | round }}
<!-- Returns 1235 -->

{{ 1234.56789 | round : 3 : RoundType.Floor }}
<!-- Returns 1234.567 -->

{{ 9876.54321 | round : 2 : RoundType.Ceil }}
<!-- Returns 9876.54 -->
```


### sqrt

Returns the square root of given number.
Expand Down
26 changes: 26 additions & 0 deletions packages/pipes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export class YourComponent {
- [min](#min)
- [pct](#pct)
- [pow](#pow)
- [round](#round)
- [sqrt](#sqrt)
- [sum](#sum)
- [Generic Pipes](#generic-pipes)
Expand Down Expand Up @@ -375,6 +376,31 @@ Usage: `base | pow [:exponent]`
```


### round

Returns the rounded value of given number. By default the value is rounded to the nearest integer.

It also accepts an optional argument `RoundType` for rounding the value up or down.\
`RoundType.Default` = Default rounding as in `Math.round()`
`RoundType.Floor` = Round down as in `Math.floor()`
`RoundType.Ceil` = Round up as in `Math.ceil()`

Optionally, the number of decimal places to which the result should be rounded may also be specified.

Usage: `number | round [:decimalPlaces][:roundType]`

```html
{{ 1234.56789 | round }}
<!-- Returns 1235 -->

{{ 1234.56789 | round : 3 : RoundType.Floor }}
<!-- Returns 1234.567 -->

{{ 9876.54321 | round : 2 : RoundType.Ceil }}
<!-- Returns 9876.54 -->
```


### sqrt

Returns the square root of given number.
Expand Down
1 change: 1 addition & 0 deletions packages/pipes/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ export { MaxPipe } from './number/max/max.pipe';
export { MinPipe } from './number/min/min.pipe';
export { PctPipe } from './number/pct/pct.pipe';
export { PowPipe } from './number/pow/pow.pipe';
export { RoundPipe, RoundType } from './number/round/round.pipe';
export { SqrtPipe } from './number/sqrt/sqrt.pipe';
export { SumPipe } from './number/sum/sum.pipe';
26 changes: 26 additions & 0 deletions packages/pipes/src/lib/number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A collection of pipes exported by `NglrxNumberPipesModule`.
- [min](#min)
- [pct](#pct)
- [pow](#pow)
- [round](#round)
- [sqrt](#sqrt)
- [sum](#sum)

Expand Down Expand Up @@ -86,6 +87,31 @@ Usage: `base | pow [:exponent]`
```


### round

Returns the rounded value of given number. By default the value is rounded to the nearest integer.

It also accepts an optional argument `RoundType` for rounding the value up or down.\
`RoundType.Default` = Default rounding as in `Math.round()`
`RoundType.Floor` = Round down as in `Math.floor()`
`RoundType.Ceil` = Round up as in `Math.ceil()`

Optionally, the number of decimal places to which the result should be rounded may also be specified.

Usage: `number | round [:decimalPlaces][:roundType]`

```html
{{ 1234.56789 | round }}
<!-- Returns 1235 -->

{{ 1234.56789 | round : 3 : RoundType.Floor }}
<!-- Returns 1234.567 -->

{{ 9876.54321 | round : 2 : RoundType.Ceil }}
<!-- Returns 9876.54 -->
```


### sqrt

Returns the square root of given number.
Expand Down
2 changes: 2 additions & 0 deletions packages/pipes/src/lib/number/nglrx-number-pipes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { MaxPipe } from './max/max.pipe';
import { MinPipe } from './min/min.pipe';
import { PctPipe } from './pct/pct.pipe';
import { PowPipe } from './pow/pow.pipe';
import { RoundPipe } from './round/round.pipe';
import { SqrtPipe } from './sqrt/sqrt.pipe';
import { SumPipe } from './sum/sum.pipe';

Expand All @@ -16,6 +17,7 @@ const NUMBER_PIPES = [
MinPipe,
PctPipe,
PowPipe,
RoundPipe,
SqrtPipe,
SumPipe,
];
Expand Down
52 changes: 52 additions & 0 deletions packages/pipes/src/lib/number/round/round.pipe.spec.ts
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);
});

});
28 changes: 28 additions & 0 deletions packages/pipes/src/lib/number/round/round.pipe.ts
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;
}
}

}

0 comments on commit 23ad7af

Please sign in to comment.