NumberConverstion Directives
This functionalities is used to convert to string to number. so here this will convert to number, price and currency.
Save this directives and name it as numberConversion
@HostListener('keyup', ['$event']) onKeyUp(event) {
let value = this. eleRef.nativeElement.value;
value = this. functionCall(value);
this. eleRef.nativeElement.value = convertNumber(value);
this. eleRef.nativeElement.value = convertPrice(value);
this. eleRef.nativeElement.value = convertCurrency(value);
}
@HostListener('paste', ['$event']) onPaste(event) {
this. eleRef.nativeElement.value =
this. functionCall(_.replace(this. eleRef.nativeElement.value, /[^a-zA-Z0-9 ]/g, ''));
}
functionCall(value) {
value = _.replace(value, /^\s/g, '');
value = _.replace(value, /[^0-9 ]/g, '');
return value;
}
Convert to number
convertNumber(value) {
value = Number().toLocaleString();
return value
}
Convert to price value
convertPrice(value) {
const lastString = value.substr(value.length - 1);
value = parseFloat(value);
value = value.toLocaleString('en-US', {maximumFractionDigits: 3});
return value;
}
Convert to currency
convertCurrency(value) {
value = value.toLocaleString('en-US');
return value;
}
0 Comments