Zero removed after decimal point while Observable emitted the next value Angular

33 views Asked by At

Here, In this example An observable is emitting values. But in console output, zeros after decimals are removed automatically.

I want to keep these zeros as it as after decimal. Is there a way to keep this zeros?

const numbers$ = of(1.440000, 2.40000, 3.0000);

numbers$.subscribe(
     value => console.log('Observable emitted the next value- ' + value)
);

Output:

Observable emitted the next value- 1.44   // need output as 1.440000
Observable emitted the next value- 2.4   // need output as 2.40000
Observable emitted the next value- 3    // need output as 3.0000
1

There are 1 answers

4
Християн Христов On

This is not a rxjs issue, this is js behaviour.

let numbers = [1.44000,1.20000,1.34234234];

numbers.forEach(x => console.log(x));

let precission = 4;

numbers.forEach(x => console.log(Number(x).toFixed(precission
)));

To receive the desired outcome you can use toFixed as seen here How to add a trailing zero to a price?. Keep in mind that this will cast the number to a string, so I would advise to add the "padding" zeroes as a last step, or even better by using the DecimalPipe in the templates, where you want to show the values.