angular 如果输入中没有小数点,则扩展货币管道未格式化 string/int
angular extended currency pipe in is not formatting string/int if there is no decimal point in input
Angular 如果数字是字符串格式并且字符串中没有小数点,则货币管道不会将 string/int 转换为货币格式。
假设金额是 12,我想显示 12.00 美元,如果传递了“12”,它不会显示,但如果传递了 12.00,它就可以正常工作。
//Code
import {Pipe, PipeTransform} from "@angular/core";
import {CurrencyPipe} from "@angular/common";
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(-(\d+))?)?$/;
@Pipe({name: 'myCurrency'})
export class MyCurrencyPipe implements PipeTransform {
constructor (private _currencyPipe: CurrencyPipe) {}
transform(value: any, currencyCode: string, symbolDisplay: boolean, digits: string): string {
if (typeof value === 'number' || _NUMBER_FORMAT_REGEXP.test(value)) {
return this._currencyPipe.transform(value, currencyCode, symbolDisplay, digits);
} else {
return value;
}
}
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
<div>{{priceNoDecimal}}</div> {{priceNoDecimal | myCurrency}}
<div>{{priceWithDecimal}}</div> {{priceWithDecimal | myCurrency}}
</div>
`,
})
export class App {
name:string;
priceWithDecimal: string;
priceNoDecimal: string;
constructor() {
this.name = 'Angular2',
this.priceNoDecimal = "12"
this.priceWithDecimal = "12.00"
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App , MyCurrencyPipe],
providers: [CurrencyPipe],
bootstrap: [ App ]
})
export class AppModule {}
//output
Hello Angular2
12
12
12.00
USD12.00
如果您查看已应用的正则表达式:/^(\d+)?\.((\d+)(-(\d+))?)?$/
它需要一个小数点。
以下正则表达式使小数点可选 /^(\d+)?\.?((\d+)(-(\d+))?)?$/
如果没有上下文,问题可能不清楚。来自 is the regex that is used in original number pipes 的管道检测字符串中的数字:
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(-(\d+))?)?$/;
为了更接近input conditioning of original currency pipe,管道可以更改为:
function isNumeric(value: any): boolean {
return !isNaN(value - parseFloat(value));
}
@Pipe({name: 'looseCurrency'})
export class LooseCurrencyPipe implements PipeTransform {
constructor(private _currencyPipe: CurrencyPipe) {}
transform(value: any, currencyCode: string, symbolDisplay: boolean, digits: string): string {
value = typeof value === 'string' && isNumeric(value) ? +value : value;
if (typeof value === 'number') {
return this._currencyPipe.transform(value, currencyCode, symbolDisplay, digits);
} else {
return value;
}
}
}
其中 isNumeric
是使用此方法从 framework internals. It should work fine 中提取的辅助函数。
Angular 如果数字是字符串格式并且字符串中没有小数点,则货币管道不会将 string/int 转换为货币格式。
假设金额是 12,我想显示 12.00 美元,如果传递了“12”,它不会显示,但如果传递了 12.00,它就可以正常工作。
//Code
import {Pipe, PipeTransform} from "@angular/core";
import {CurrencyPipe} from "@angular/common";
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(-(\d+))?)?$/;
@Pipe({name: 'myCurrency'})
export class MyCurrencyPipe implements PipeTransform {
constructor (private _currencyPipe: CurrencyPipe) {}
transform(value: any, currencyCode: string, symbolDisplay: boolean, digits: string): string {
if (typeof value === 'number' || _NUMBER_FORMAT_REGEXP.test(value)) {
return this._currencyPipe.transform(value, currencyCode, symbolDisplay, digits);
} else {
return value;
}
}
}
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
<div>{{priceNoDecimal}}</div> {{priceNoDecimal | myCurrency}}
<div>{{priceWithDecimal}}</div> {{priceWithDecimal | myCurrency}}
</div>
`,
})
export class App {
name:string;
priceWithDecimal: string;
priceNoDecimal: string;
constructor() {
this.name = 'Angular2',
this.priceNoDecimal = "12"
this.priceWithDecimal = "12.00"
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App , MyCurrencyPipe],
providers: [CurrencyPipe],
bootstrap: [ App ]
})
export class AppModule {}
//output
Hello Angular2
12
12
12.00
USD12.00
如果您查看已应用的正则表达式:/^(\d+)?\.((\d+)(-(\d+))?)?$/
它需要一个小数点。
以下正则表达式使小数点可选 /^(\d+)?\.?((\d+)(-(\d+))?)?$/
如果没有上下文,问题可能不清楚。来自
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(-(\d+))?)?$/;
为了更接近input conditioning of original currency pipe,管道可以更改为:
function isNumeric(value: any): boolean {
return !isNaN(value - parseFloat(value));
}
@Pipe({name: 'looseCurrency'})
export class LooseCurrencyPipe implements PipeTransform {
constructor(private _currencyPipe: CurrencyPipe) {}
transform(value: any, currencyCode: string, symbolDisplay: boolean, digits: string): string {
value = typeof value === 'string' && isNumeric(value) ? +value : value;
if (typeof value === 'number') {
return this._currencyPipe.transform(value, currencyCode, symbolDisplay, digits);
} else {
return value;
}
}
}
其中 isNumeric
是使用此方法从 framework internals. It should work fine 中提取的辅助函数。