Angular 使用管道键入脚本/角度逗号或小数点
我创建了自己的自定义管道,并希望添加一个或多个。到千值例如:1000转换为1000或1.000 这是我的自定义管道:Angular 使用管道键入脚本/角度逗号或小数点,angular,typescript,decimal,Angular,Typescript,Decimal,我创建了自己的自定义管道,并希望添加一个或多个。到千值例如:1000转换为1000或1.000 这是我的自定义管道: import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'decimalUnitPipe' }) export class DecimalUnitPipe implements PipeTransform { public transform(value: any) {
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'decimalUnitPipe'
})
export class DecimalUnitPipe implements PipeTransform {
public transform(value: any) {
}
}
有没有关于如何做到这一点的想法?使用正则表达式将您的值转换为千表示法。这就是您的
转换
函数应具备的功能:
你能给我解释一下这条线是干什么的吗替换(/\B(?=(\d{3})+(?!\d))/g,“,”)这是一个正则表达式,在
值的每3位后面插入,
,非常感谢。还有一个问题。如果我有一个数字,例如1000,我想在逗号35后面加上数字,那么这个数字应该是1.000,35或1000.35。我怎么做?如果你写{'1000,35'| decimalUnitPipe}
,那么它将返回1000,35
我需要将这个管道应用到我正在工作的应用程序的输入上。所以有时候你会喜欢:$1.000,35,35代表(美分)或$1000.35
public transform(value: any) {
return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");;
}