1

My application accepts different language. I have German language preferred by user and have it registered using registerLocale. I am able to convert the DB decimal values - 0.001 (en-fromat) to 0,001 (de format)

But now when the user has entered any number/ decimal in their regional format then I need to change that from 0,001 (de format) to 0.001 (en format) so that the correct data is stored in the DB.

I have tried following -

    console.log(this.value.toLocaleString('de-DE')); -- doesnt work
    console.log(new Intl.NumberFormat('de-DE').format(this.value)); --give NaN
    new Intl.NumberFormat(locale, {minimumFractionDigits: 5}).format(Number(value)); - gives NaN
    console.log('formatNumber-' + this.LocalNumberPipe.transform(value)); -- give NaN

I am not understanding how to resolve this. Is there any generic way. I am looking for a way which will help me in converting any locale to EN-US as German locale is one such example.

My localNumberPipe is

@Pipe({
    name: 'localNumber',
})
export class LocalNumberPipe implements PipeTransform {

    constructor(private session: SessionService) { }

    transform(value: any, format?: string) {
        if (value == null) { return ''; } // !value would also react to zeros.
        if (!format) { format = '.2-2'; }

        return formatNumber(value, this.session.locale, format);
    }
}

Please, do any one have any idea on how can i solve this.

Anna
  • 41
  • 1
  • 9

1 Answers1

0

you can simply change , to . then parse it To Float :

this.value.replace(",", "."); // to reaplace ',' with '.'
let result = parseFloat(this.value); 
aziz k'h
  • 775
  • 6
  • 11
  • I dont think this will work in all the cases. Cause since my input type is text to support comma. If i get inputs like - `1,200.50 (input in german format) it will give me 1 but it should give 1.20050 in english format` – Anna Oct 16 '20 at 16:05
  • then you should add test after replace `if(!isNaN(parseFloat(this.value)))` or you simply control the input to be like you want – aziz k'h Oct 16 '20 at 16:05
  • I have find this on stackoverflow it works fine. https://stackoverflow.com/questions/29255843/is-there-a-way-to-reverse-the-formatting-by-intl-numberformat-in-javascript – Anna Oct 16 '20 at 16:06
  • Yes I was working on creating and handling it using regex but its very difficult as we have 1000 of locales supported in our system. So I suppose the link in above comment should work. Please refer to second answer. – Anna Oct 16 '20 at 16:08
  • i create angular app in stackBlitz with mat-form-field that verif inuput as you went [here]:(https://stackblitz.com/edit/angular-9-material-starter-6ageyk?file=src/app/app.component.ts) – aziz k'h Oct 16 '20 at 16:28