Angular2货币管道更改小数点分隔符 [英] Angular2 Currency Pipe change decimal separator

查看:641
本文介绍了Angular2货币管道更改小数点分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好角朋友,

我正在研究一个angular2应用程序(实际上是多个)。我住在荷兰。

目前我正在用我的货币格式化如下:

{{ someIntegerWithCentsToBeDivided / 100 |货币:'EUR':true:'1.0-2'}}

显示500为Eurosign 5,501为Eurosign 5.01。



现在我们的荷兰人真的很喜欢逗号,所以有人知道如何改变。如果有人知道如何显示5,奖励分数 - 可选地,当没有分。
我现在的想法是扩展CurrencyPipe

解决方案

您的问题可能在前一段时间已经解决了,以供其他荷兰开发者(比如我自己)参考:

创建一个自定义管道:

 从'@ angular / core'导入{Pipe}; 
$ b @ bipe({
name:'currencyFormat'
})
export class CurrencyFormat {
transform(value:number,
currencySign :string ='€',
decimalLength:number = 2,
chunkDelimiter:string ='。',
decimalDelimiter:string =',',
chunkLength:number = 3 ):string {

value / = 100;

let result ='\\d(?=(\\d {'+ chunkLength +'})+'+(decimalLength> 0?'\\D' :'$')+')'
let num = value.toFixed(Math.max(0,~~ decimalLength));

return currencySign +(decimalDelimiter?num.replace('。',decimalDelimiter):num).replace(new RegExp(result,'g'),'$&'+ chunkDelimiter);




$ b $现在你可以使用:

  {{someIntegerWithCentsToBeDivided |这个管道已经包含了所有的荷兰默认值,但是你可以很容易的修改或者使用它们他们作为模板中的参数。例如: 

  {{1234567 | currencyFormat:'$',2,'','。',3}} 

code> $ 12 345.67 作为输出。


Hello angular friends,

I'm working on an angular2 app (multiple actually). And I live in the Netherlands.

Currently I'm formatting my currency with the following:

{{someIntegerWithCentsToBeDivided / 100 | currency:'EUR':true:'1.0-2'}}

This displays something like 500 to be Eurosign 5 and 501 to be Eurosign 5.01.

Now we dutchies really like comma's the other way around so does anyone know how to change the . to a ,?

Bonus points if someone knows how to show 5,- optionally when there is no cents. My thoughts now would be to extend the CurrencyPipe

解决方案

Your problem has probably been solved some time ago, but just for reference for other Dutch developers (like myself):

Create a custom Pipe:

import {Pipe} from '@angular/core';

@Pipe({
    name: 'currencyFormat'
})
export class CurrencyFormat {
    transform(value: number,
        currencySign: string = '€ ',
        decimalLength: number = 2, 
        chunkDelimiter: string = '.', 
        decimalDelimiter:string = ',',
        chunkLength: number = 3): string {

        value /= 100;

        let result = '\\d(?=(\\d{' + chunkLength + '})+' + (decimalLength > 0 ? '\\D' : '$') + ')'
        let num = value.toFixed(Math.max(0, ~~decimalLength));

        return currencySign+(decimalDelimiter ? num.replace('.', decimalDelimiter) : num).replace(new RegExp(result, 'g'), '$&' + chunkDelimiter);
    }
}

Now you can use:

{{someIntegerWithCentsToBeDivided | currencyFormat}}

The Pipe has already all the Dutch defaults included, but you can easily change them or use them as arguments in the template. For example:

{{1234567 | currencyFormat:'$',2,' ','.',3}}

will give $12 345.67 as output.

这篇关于Angular2货币管道更改小数点分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆