3

I'm using angular decimalPipe

https://angular.io/api/common/DecimalPipe

What I need is at least one Integer, and from 0 to 2 decimals, so, following the link, it is

number: '1.0-2'

If I use it in a label, as static value, it works fine, my problem is using it in an input, the pipe does not work properly

It is included in an input, it works if the third decimal is 5 or higher, but not if it is lower, here is a working example:

https://stackblitz.com/edit/angular-tlesbo?file=src%2Fapp%2Fapp.component.html

<input type="number" matInput
        [ngModel]="value | number:'1.0-2'" (ngModelChange)="value=$event" />

Test including for example 2.5222 it won't be formated and keeps at it is, but 2.548 will be and is modify to 2.55.

Am I using bad the pipe?

cucuru
  • 3,456
  • 8
  • 40
  • 74
  • It will help if you could narrow down what is the issue and what are you expecting out of the pipe? – codingbbq Jan 29 '19 at 10:32
  • What browser are you on? Your implementation works like a charm for me in firefox ? – Zze Jan 29 '19 at 10:34
  • I'm using chrome, but I can see it's not working in firefox, the number is not format, is deleted from the input – cucuru Jan 29 '19 at 10:37
  • @noobcode I want it to work as described in the angular link. It works if it's a label, but not if it's an input. – cucuru Jan 29 '19 at 11:00

1 Answers1

0

You need to create your custom pipe as DecimalPipe doesn't provide any floor feature.


There is a answer about this already (here). Better post an issue at the Angular's Github page. You can create a Pipe, but the FloorPipe should already be at the DecimalPipe's options I think.
The other answer teach you how to crate a FloorPipe and how to use it however.

Gaspar
  • 1,515
  • 13
  • 20
  • I don't need a floor, I need to format the number, as usual, I mean, at least one integer, and from 0 to 2 decimal, if it's not an input but a label, it works. – cucuru Jan 29 '19 at 10:51
  • if you don't need a floor the pipe is working normally, put a `
     {{ value }} 
    ` below your `input` and write _0.5_, if you press _4_ it will keep putting 4 and your model will be wrong (more them two after comma), if you press _5_ it will round it and keep two after comma. So, you need a floor function to keep it 4 or create a pipe that check if third number after comma < 5 delete it
    – Gaspar Jan 29 '19 at 13:19