2017-10-24 5 views
-2

私は何千ものポイントを入れるためにフォーマットする必要があります、私は角度2のパイプを使用してそれをフォーマットする必要があります。Ejem:数は1983839であり、結果は1.983.839でなければなりません?ここでフォーマット番号は何千ものポイントを置く

+2

パイプで試してみたものが歓迎されます –

+0

これはロケールに依存します(ここではen-GBでは1,983,839を指定します)。 Angularはすでにパイプを提供しています:['DecimalPipe'](https://angular.io/api/common/DecimalPipe)。 – jonrsharpe

+0

ロケールがes-ES – ararb78

答えて

0

カスタムパイプ

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] 
}) 
+0

の場合これがカスタムパイプを必要とするのはなぜだと思いますか?千単位区切り記号はロケール設定の一部です。 – jonrsharpe

+0

私はパイプのコンストラクタでsuperにローカルを追加して解決しました。このようにして置換を避けます。パイプのコンストラクタで:コンストラクタ(){ super( "es-ES"); } – ararb78

+0

なぜ私のモジュールのプロバイダにカスタムパイプを登録する必要がありますか? – ararb78

0

をカスタムパイプを登録する必要があります。

export class appComponent{ 
    num:number = 1983839; 
    printNum:any; 
    constructor() { 
    this.test = this.num.toLocaleString('es-CO'); 
    console.log('printNum',this.printNum); 
    } 
} 
0

私は問題を解決このソリューションで私は角度を提供する "DecimalPipe"パイプを使用しました。

私はプロバイダとインポート "LOCALE_ID" でapp.moduleで私のロケールを追加しました:

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

providers: [ 
    { provide: LOCALE_ID, useValue: "es-ES" }, 
    ... 
] 

をその後、私のコンポーネントで、私は角度のパイプ "DecimalPipe" を使用:

component.ts: 
    import { DecimalPipe } from '@angular/common'; 

component.html: 
    {{fila.NumeroCopiasBN | Number:'1.0-0' }} 

私がパイプ '1.0-0'に送るパラメータは、以下のフォーマットを持つ文字列です:

{minFractionDigits} - {maxFractionDigits}

+0

コンポーネントをパイプで 'import 'する必要はありませんテンプレート。また、それは 'number'です。すべて小文字です。 – jonrsharpe

+0

@jonrsharpe ok、私はコンポーネントのパイプのインポートを削除しました。 tnx – ararb78

関連する問題