Open In App

Angular10 getLocaleCurrencyCode() Function

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see what is getLocaleCurrencyCode in Angular 10 and how to use it.

The getLocaleCurrencyCode is used to get the default currency code for the given locale.

Syntax:

getLocaleCurrencyCode(locale: string): string | null

NgModule: Module used by getLocaleCurrencyCode is:

  • CommonModule

Approach:

  • Create the angular app to be used
  • In app.module.ts import LOCALE_ID because we need locale to be imported for using get getLocaleCurrencyCode.
import { LOCALE_ID, NgModule } from '@angular/core';
  • In app.component.ts import getLocaleCurrencyCode and LOCALE_ID
  • inject LOCALE_ID as a public variable.
  • In app.component.html show the local variable using string interpolation
  • serve the angular app using ng serve to see the output.

Parameters:

  • locale: A string containing locale code with rules.

Return value:

  • string : string containing the currency symbol.

Example 1:

app.module.ts




import { LOCALE_ID, NgModule } 
        from '@angular/core';
import { BrowserModule } 
        from '@angular/platform-browser';
  
import { AppRoutingModule } 
        from './app-routing.module';
import { AppComponent } 
        from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [
      { provide: LOCALE_ID, useValue: 'en-GB' },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }


app.component.ts




import {FormStyle,
        getLocaleCurrencyCode, 
        TranslationWidth } 
        from '@angular/common';
  
import { Component, 
          Inject,OnInit, 
          LOCALE_ID } 
          from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    code = getLocaleCurrencyCode(this.locale);
    constructor(
        @Inject(LOCALE_ID) public locale: string,){}
      }


app.component.html




<h1>
   GeeksforGeeks
</h1>
<p>Currency code is : {{code}}</p>


Output:

Example 2:

app.module.ts




import { LOCALE_ID, NgModule } 
        from '@angular/core';
import { BrowserModule } 
        from '@angular/platform-browser';
  
import { AppRoutingModule } 
        from './app-routing.module';
import { AppComponent } 
        from './app.component';
  
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  providers: [
      { provide: LOCALE_ID, useValue: 'en-GB' },
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }


app.component.ts




import {FormStyle,
        getLocaleCurrencyCode, 
        TranslationWidth } 
        from '@angular/common';
  
import {Component, 
        Inject, 
        LOCALE_ID } 
        from '@angular/core';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
export class AppComponent {
    code = getLocaleCurrencyCode(this.locale);
    constructor(
        @Inject(LOCALE_ID) public locale: string,){}
      }


app.component.html




<h1>
   GeeksforGeeks
</h1>
<p>Currency code is : {{code}}</p>
  
<p>
    {{code}}100
    {{code}}200 
    {{code}}300
    {{code}}400
    {{code}}500
</p>


Output:

Reference: https://angular.io/api/common/getLocaleCurrencyCode



Last Updated : 29 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads