Angular 设置数字格式以将点数设置为千
我需要设置一个数字的格式,以千为单位,我需要使用角度为2的管道来设置它的格式。Ejem:号码是1983839,结果一定是1.983.839,知道吗Angular 设置数字格式以将点数设置为千,angular,format,pipe,Angular,Format,Pipe,我需要设置一个数字的格式,以千为单位,我需要使用角度为2的管道来设置它的格式。Ejem:号码是1983839,结果一定是1.983.839,知道吗 {{amount | number}} 这是定制烟斗 import { Pipe } from '@angular/core'; import {DecimalPipe} from "@angular/common"; @Pipe({ name: 'pointReplacer' }) export class PointReplacerP
{{amount | number}}
这是定制烟斗
import { Pipe } from '@angular/core';
import {DecimalPipe} from "@angular/common";
@Pipe({
name: 'pointReplacer'
})
export class PointReplacerPipe extends DecimalPipe {
transform(value: any, digits?: string): string {
return super.transform(value, digits).replace(',', '.');
}
}
您需要将自定义管道注册到模块中
@NgModule({
declarations: [PointReplacerPipe],
providers: [PointReplacerPipe]
})
在下面提到的组件代码中:
export class appComponent{
num:number = 1983839;
printNum:any;
constructor() {
this.test = this.num.toLocaleString('es-CO');
console.log('printNum',this.printNum);
}
}
我用这个解决方案解决了这个问题。我使用了“分米管”管道,提供角度 我在providers中的app.module中添加了我的语言环境,并导入了“locale\u ID”: 然后在我的组件中,我使用了angular的管道“DecimalPipe”:
component.ts:
import { DecimalPipe } from '@angular/common';
component.html:
{{fila.NumeroCopiasBN | Number:'1.0-0' }}
我发送到管道“1.0-0”的参数是具有以下格式的字符串:
{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}可以尝试使用管道,这将取决于区域设置(在en GB中,我们将使用1983839)。Angular已经为它提供了一个管道:。区域设置为ESES。为什么您认为这需要一个自定义管道?数千分隔符只是区域设置配置的一部分。我在de-pipe的构造函数中添加了local到super来解决它,这样就避免了替换。在de pipe的构造函数中:constructor(){super(“es”);}为什么需要在模块的提供程序中注册我的自定义管道?@ararb78但是,如果您所做的只是将区域设置传递给父级,那么您不需要自定义管道。数字将自动格式化以匹配用户的区域设置。您不需要将其注册为提供程序,除非您计划将其注入到某个地方。@jonrsharpe是的,我不需要自定义管道,因为在提供程序中包含app.module的区域设置“{provide:locale_ID,useValue:“es es es”}”和设置{fila.data | Number:'1.0-0'}”有效。您不需要导入组件中的管道,就可以在模板中使用它。它也是
编号
,全小写。@jornsharpe好的,我删除了组件中的导入管道。tnx
component.ts:
import { DecimalPipe } from '@angular/common';
component.html:
{{fila.NumeroCopiasBN | Number:'1.0-0' }}