angular - 以角度 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);
}
}
拜托,有没有人知道我该如何解决这个问题。
解决方案
您可以简单地将 , 更改为 . 然后将其解析为 Float :
this.value.replace(",", "."); // to reaplace ',' with '.'
let result = parseFloat(this.value);
推荐阅读
- mysql - 如何在 WHERE 子句已编入索引的情况下加速这个 MySQL 查询?
- html - 如何使用自动换行属性限制元素的宽度?
- reactjs - 反应道具类型验证错误
- javascript - TabIndex 不适用于 RadWindow 中的输入元素
- mysql - NOT IN key word not working following mysql query ?
- mysql - 由于编码使用不当而导致数据保护受损?
- python-3.x - PySpark parquet 数据类型
- google-api - 通过 API 获取所有委托联系人
- sql - SQL Query Help - Negative reporting
- maple - why map on list generates extra []