从第4章开始的正确和默认方法:
<p>{{99 | number:'1.3-3'}}</p>
<!--output '99.000'-->
digitInfo是一个字符串,其格式如下:
{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
minIntegerDigits
要使用的最小整数位数。
Defaults to 1
minFractionDigits
小数后的最小位数。
Defaults to 0
.
maxFractionDigits
小数后的最大位数。
Defaults to 3
.
由于角度5或6(不确定),您还可以提供
文化
.
<p>{{99 | number:'1.3-3':CultureType.de_DE}}</p>
<!--output '99,000'-->
import { registerLocaleData } from "@angular/common";
import localeDe from '@angular/common/locales/de';
import localeFr from '@angular/common/locales/fr';
import localeNl from '@angular/common/locales/nl';
import localeEnGb from '@angular/common/locales/en-GB';
import localeEnUs from '@angular/common/locales/en';
registerLocaleData(localeDe, CultureType.Development);
registerLocaleData(localeFr, CultureType.fr_FR);
registerLocaleData(localeNl, CultureType.nl_NL);
registerLocaleData(localeEnGb, CultureType.en_GB);
registerLocaleData(localeEnUs, CultureType.en_US);
registerLocaleData(localeDe, CultureType.de_DE);
export class CultureType {
public static readonly Development: 'xh-ZA' = 'xh-ZA';
public static readonly de_DE: 'de-DE' = 'de-DE';
public static readonly nl_NL: 'nl-NL' = 'nl-NL';
public static readonly fr_FR: 'fr-FR' = 'fr-FR';
public static readonly en_GB: 'en-GB' = 'en-GB';
public static readonly en_US: 'en-US' = 'en-US';
}
你想吗
对于函数中的区域性,这是可能的,因为6:
import { formatNumber, getLocaleNumberSymbol, NumberSymbol } from "@angular/common";
/**
* DigitsInfo:
* {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
* 1.2-2 => 1,00
*/
public formatNumber(num: number, digitsInfo: string): string {
const culture: string = this.languageService.getCulture();
return formatNumber(num, culture, digitsInfo);
}
你想吗
将字符串解析为数字
再一次:
import { formatNumber, getLocaleNumberSymbol, NumberSymbol } from "@angular/common";
public parseStringNumber(str: string): number | null {
if (!str) {
return null;
}
const culture: string = this.languageService.getCulture();
const decimalSeparator: string = getLocaleNumberSymbol(culture, NumberSymbol.Decimal);
const groupSeparator: string = getLocaleNumberSymbol(culture, NumberSymbol.Group);
const decimalSeparatorRegex = new RegExp('\\' + decimalSeparator, 'g');
const groupSeparatorRegex = new RegExp('\\' + groupSeparator, 'g');
const normalizedString = str.replace(groupSeparatorRegex, '').replace(decimalSeparatorRegex, '.');
return parseFloat(normalizedString);
}