angularkendo-uikendo-angular-ui

Angular pipe, custom decimal numbers


having

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

How can I display myNumber using myPrecision?

<label> {{ myNumber | ???myPrecision }} </label>

Solution

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