首页 > 解决方案 > 以角度 8 将德语区域设置数字格式 (0,01) 转换为英语区域设置格式 (0.01)

问题描述

我的应用程序接受不同的语言。我有用户首选的德语,并使用 registerLocale 进行了注册。我能够将 DB 十进制值 - 0.001(en-fromat)转换为 0,001(de 格式)

但是现在当用户以他们的区域格式输入任何数字/小数时,我需要将其从 0,001(de 格式)更改为 0.001(en 格式),以便将正确的数据存储在数据库中。

我试过以下 -

    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

我不明白如何解决这个问题。有没有通用的方法。我正在寻找一种方法来帮助我将任何语言环境转换为 EN-US,因为德国语言环境就是这样一个例子。

我的 localNumberPipe 是

@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);
    }
}

拜托,有没有人知道我该如何解决这个问题。

标签: angulartypescriptinternationalizationlocale

解决方案


您可以简单地将 , 更改为 . 然后将其解析为 Float :

this.value.replace(",", "."); // to reaplace ',' with '.'
let result = parseFloat(this.value); 

推荐阅读