Bastien Donjon

Développeur web à Bordeaux

Tuesday

6

September 2016

0

COMMENTS

Round number with Angular 2, pipe and Typescript

Written by , Posted in Javascript, typescript

// round.pipe.ts
import {Pipe, PipeTransform} from "@angular/core";

/**
 *
 */
@Pipe({name: 'round'})
export class RoundPipe implements PipeTransform {
    /**
     *
     * @param value
     * @returns {number}
     */
    transform(value: number): number {
        return Math.round(value);
    }
}
// app.module.ts
@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        routing
    ],
    declarations: [
        AppComponent,
        RoundPipe
    ],
    providers: [
        BaseRequestOptions
    ],
    bootstrap: [AppComponent]
})
<!-- exemple -->
<span>{{ yournumber | round }}</span>

Builded with TypeScript 1.8 for Angular 2.RC5