My application supports multiple languages. A user has selected German as their preferred language and I have registered it using registerLocale. I am able to convert decimal values from 0.001 (in English format) to 0,001 (in German format).
However, when a user enters a number or decimal in their regional format, such as 0,001 (in German format), I need to convert it to 0.001 (in English format) before storing the data in the database.
I have tried various methods, including:
console.log(this.value.toLocaleString('de-DE')); -- does not work
console.log(new Intl.NumberFormat('de-DE').format(this.value)); -- returns NaN
new Intl.NumberFormat(locale, {minimumFractionDigits: 5}).format(Number(value)); - returns NaN
console.log('formatNumber-' + this.LocalNumberPipe.transform(value)); -- returns NaN
I am struggling to find a solution to this issue. Is there a generic way to convert any locale to EN-US, as German is just one example?
Below is my LocalNumberPipe:
@Pipe({
name: 'localNumber',
})
export class LocalNumberPipe implements PipeTransform {
constructor(private session: SessionService) { }
transform(value: any, format?: string) {
if (value == null) { return ''; } // Returns empty string for null values
if (!format) { format = '.2-2'; }
return formatNumber(value, this.session.locale, format);
}
}
If anyone has any ideas on how to solve this problem, please share. Thank you.