2

I have two inputs for the user. The first input is the main number. The second input is the number of zeroes behind the main number.

The basic {{ mainNumber | number: '1.5'}}works. Here I will have five zeroes behind the main number. But I want that 5 to be dynamic, to be what the user has input in the second field.

This is the component.html:

  Number <input type="number" [(ngModel)]="mainNumber" placeholder="Enter the number"> &nbsp;
  Zero: <input type="number" [(ngModel)]="zeroNumber" placeholder="Enter the zero"> <br>
  <hr>
  <b> Number: </b>{{mainNumber}} &nbsp;
  <b> Zero: </b> {{zeroNumber}} <br>
  <!-- {{ mainNumber | number: '1.10'}} -->

  Final number: {{ mainNumber | mojpipe }}

This is the component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  mainNumber: number = 0;
  zeroNumber: number = 0;
  title = 'shopping-project';
}

This is the custom pipe I tried to build, mypipe.ts:

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

@Pipe({ name: 'mojpipe' })
export class AddZero implements PipeTransform{
    testTwo: number = 1;
    transform(size: number){
        return (size + "."+ this.testDva);
    };
}

1 Answer 1

5

Well, you can use the variable inside your pipe expression:

{{ mainNumber | number: ('1.' + zeroNumber }}
Sign up to request clarification or add additional context in comments.

2 Comments

I just get this error in the console: mainNumber is not a number' for pipe 'DecimalPipe'
I fixed the old answer. This worked great for me: Final number: {{ mainNumber | number: ('1.' + zeroNumber) }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.