How can i use pipe in this case

Viewed 54

i have this number coming from my api

"vlr": 1749109.14

and i have to formate to show

174.910.914

I tried using pipe but I couldn't get the formatting I wanted.

is there a way to get this formatting, or would it be better to change the value coming from the api?

1 Answers

According to me you have 2 solutions :

    1. Create a function into the service you're receiving this data, and format directly into the service.
    1. Create a pipe and calculate the value.
  1. (service-name)
getData(): Observable<any> {
  return this.http.get<any>(...).pipe(
    map(res => this.formatValue(res.vlr))
  );
}

private formatValue(value: number): string {
  let temp = value.toString().replace('.', '');
  return temp.match(/.{1,3}/g).join(".");
}

(component)


vlr: string;

constructor(service: ServiceName) {
  this.service.getData().subscribe(res => {
    console.log(res);
    this.vlr = res.vlr;
  });
}

  1. ts
vlr = 1749109.14;

html

<span>{{ vlr | formatVlr }}</span>

(pipe)

transform(value: number): string {
  let temp = value.toString().replace('.', '');
  return temp.match(/.{1,3}/g).join(".");
}
Related