當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Angular10 formatPercent()用法及代碼示例

在本文中,我們將了解什麽是 Angular 10 中的 formatPercent 以及如何使用它。 formatPercent 用於根據區域設置規則將數字格式化為百分比。

用法:

formatPercent(value, locale, digitsInfo)

參數:

  • value:要格式化的數字。
  • locale:區域設置格式的區域設置代碼。
  • 數字信息:十進製表示選項。

返回值:

  • 細繩:格式化的百分比字符串。

NgModule:formatPercent 使用的模塊是:



  • CommonModule

方法:

  • 創建要使用的 Angular 應用程序。
  • 在 app.module.ts 中導入 LOCALE_ID,因為我們需要使用 get formatPercent 導入語言環境。
import { LOCALE_ID, NgModule } from '@angular/core';
  • 在 app.component.ts 中導入 formatPercent Zand LOCALE_ID
  • 將 LOCALE_ID 作為公共變量注入。
  • 在 app.component.html 中使用字符串插值顯示局部變量
  • 使用 ng serve 為 angular 應用程序提供服務以查看輸出。

範例1:

app.component.ts


import {
  formatPercent
 }
  from '@angular/common';
  
import {Component,
  Inject,
  LOCALE_ID }
  from '@angular/core';
  
@Component({
selector:'app-root',
templateUrl:'./app.component.html'
})
export class AppComponent {
curr = formatPercent(31,this.locale,
          '3.0-4');
constructor(
  @Inject(LOCALE_ID) public locale:string,){}
}

應用程序組件.html


<h1>
  GeeksforGeeks
</h1>
  
<p>Locale Percent is:{{curr}}</p>

輸出:

範例2:

app.component.ts




import {
  formatPercent
 }
  from '@angular/common';
  
import {Component,
  Inject,
  LOCALE_ID }
  from '@angular/core';
  
@Component({
selector:'app-root',
templateUrl:'./app.component.html'
})
export class AppComponent {
curr = formatPercent(31,this.locale,
          '3.2-4');
constructor(
  @Inject(LOCALE_ID) public locale:string,){}
}

應用程序組件.html


<h1>
  GeeksforGeeks
</h1>
  
<p>Locale Percent is:{{curr}}</p>

輸出:

範例3:

app.component.ts


import {
  formatPercent
 }
  from '@angular/common';
  
import {Component,
  Inject,
  LOCALE_ID }
  from '@angular/core';
  
@Component({
selector:'app-root',
templateUrl:'./app.component.html'
})
export class AppComponent {
curr = formatPercent(31.21,this.locale,
          '3.0-4');
constructor(
  @Inject(LOCALE_ID) public locale:string,){}
}

應用程序組件.html


<h1>
  GeeksforGeeks
</h1>
  
<p>Locale Percent is:{{curr}}</p>

輸出:

參考:https://angular.io/api/common/formatPercent




相關用法


注:本文由純淨天空篩選整理自taran910大神的英文原創作品 Angular 10 formatPercent() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。