Try this way, by using DecimalPipe,
import { Component, OnInit } from '@angular/core';
import { DecimalPipe } from '@angular/common';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
title = 'newp1';
n1 = '1'; // 1 --> 1,00
n2 = '12'; // 12 --> 12,00
n3 = '13,2' // 12,3 --> 12,30
n4 = '13,34' // 13,34 --> 12,34
constructor(private decimal: DecimalPipe) {}
ngOnInit(): void {
console.log(this.transformValue(this.n1));
console.log(this.transformValue(this.n2));
console.log(this.transformValue(this.n3));
console.log(this.transformValue(this.n4));
}
transformValue(num: string) {
return (this.decimal.transform(num.replace(',', '.'), '1.2-2', 'en'))?.replace('.', ',');
}
}
result:
1,00
12,00
13,20
13,34