4

Using angular 4,

{{31.94 | number:'1.0-0'}} 

Output: 32

Any idea, how to block the round off. Expecting the result is 31

0

5 Answers 5

7

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>
6
  • It will just floor the value, but I will be missing the comma (whatever decimal pipe doing) in case of using 1999.99, it will not gives us 1,999
    – Sudip
    Commented Nov 21, 2017 at 15:28
  • 1
    You can use multiple pipes then {{ yournumber | floor | your_decimal_pipe }} Commented Nov 21, 2017 at 15:30
  • Yes, I can write a custom pipe to do the work (floor value, comma formatted etc.) but just wanted to confirm if angular decimal pipe already have the config or not.
    – Sudip
    Commented Nov 21, 2017 at 15:32
  • No, they don't. Commented Nov 21, 2017 at 15:33
  • If you find your solution from this you can mark it correct :) Commented Nov 21, 2017 at 15:37
2

your.component.ts

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;
  }
}

your.module.ts

import {NumNotRoundPipe} from './your.component'

@NgModule({
    imports: [
    ],
    declarations: [
        NumNotRoundPipe
    ],
    entryComponents: [

    ],
    providers: [

    ],
    schemas: [CUSTOM_ELEMENTS_SCHEMA]
})

your.component.html

<span>{{yourNumber | DecimalPipe:'1.2-2'}}</span>
1

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>
0
0

You might not need an extra custom pipe. Output is a string and there are plenty of string pipes. You might truncate like this :

{{31.94 | slice:0:2}}
//Output : 31

Would achieve your desired output in that specific case.

{{1.94 | number:'2.2-2' | slice:0:2}}
//Output : 01

You'll still want to use custom pipe for now if you don't want padding 0.

1
  • What about 0.5 ?
    – N.K
    Commented Nov 26, 2020 at 13:50
-3

In your module file:

import { CommonModule } from '@angular/common';

Add it to imports array:

imports [
   CommonModule
]

Then use:

{{31.94 | number:'1.0-0'}} 
// It will display 31
1
  • this will give 32 and not 31. The decimal pipe is rounding pipe
    – Sacky San
    Commented May 30 at 1:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.