0

having

let myNumber = Math.PI;
let myPrecision = 4;

How can I display myNumber using myPrecision?

<label> {{ myNumber | ???myPrecision }} </label>
serge
  • 13,940
  • 35
  • 121
  • 205
  • 2
    https://angular.io/api/common/DecimalPipe please check that – Mateusz Ścigała Jan 25 '22 at 19:51
  • `decimalPipe.transform(this.myNumber,"1.2-2")` The digit info can be x.0-0. (x is minimum number of digits before decimal) [Usage notes](https://angular.io/api/common/DecimalPipe#usage-notes) – abestrad Jan 25 '22 at 19:53
  • please read attentively and please answer how to use `myPrecision` param, not hardcode – serge Jan 25 '22 at 19:54

1 Answers1

0

One not really elegant, but solution, could be

<div> {{ myNumber | number: '1.' + myPrecision + '-' + myPrecision + }} </div>

PS.
After the Joosep's suggestion, I made a custom pipe, like this

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'precision' })
export class PrecisionPipe implements PipeTransform {
  transform(value: number, precision: number): string {
    return value.toFixed(precision);
  }
}

so now I can use

<div> {{ myNumber | precision: myPrecision }} </div>
serge
  • 13,940
  • 35
  • 121
  • 205