Using angular 4,
{{31.94 | number:'1.0-0'}}
Output: 32
Any idea, how to block the round off. Expecting the result is 31
Using angular 4,
{{31.94 | number:'1.0-0'}}
Output: 32
Any idea, how to block the round off. Expecting the result is 31
On
import { Component, Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'DecimalPipe'})
export class NumNotRoundPipe implements PipeTransform {
transform(value: number): number {
var num1 = Math.floor(value * 100) / 100;
return num1;
}
}
import {NumNotRoundPipe} from './your.component'
@NgModule({
imports: [
],
declarations: [
NumNotRoundPipe
],
entryComponents: [
],
providers: [
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
<span>{{yourNumber | DecimalPipe:'1.2-2'}}</span>
On
When only 'round' is available, you can simulate 'floor' by substracting 0.5 to the value.
{{31.94 - 0.5 | number:'1.0-0'}}
If your value can be below 0.5, in order to avoid printing "-1", you'd have to write:
<ng-container *ngIf="myVal >= 0.5">{{ myVal - 0.5 | number:'1.0-0' }}></ng-container>
<ng-container *ngIf="myVal < 0.5">0</ng-container>
On
You need to create your custom pipe as DecimalPipe doesn't provide any floor feature. Plus you can add your decimal pipe in it. Your Custom Pipe:
@Pipe({name: 'floor'})
export class FloorPipe implements PipeTransform {
/**
*
* @param value
* @returns {number}
*/
transform(value: number): number {
return Math.floor(value);
}
}
Use in template as:
<span>{{ yournumber | floor | your_decimal_pipe }}</span>
You might not need an extra custom pipe. Output is a string and there are plenty of string pipes. You might truncate like this :
Would achieve your desired output in that specific case.
You'll still want to use custom pipe for now if you don't want padding 0.