Show the monetary value in the appropriate currency format, including fractions if the currency supports them

Here is the code snippet I am using to display the amount:

 <td>{{transaction.amount}} {{transaction.currency}}</td>

Currently, the output looks like: 56030 USD

I would like the output to be formatted like this:

<td [ngSwitch]="transaction.currency">
 <span *ngSwitchCase="'JPY'">/// Display 1000 JPY</span>
 <span *ngSwitchCase="'USD'">/// Display 10.00 USD</span>
 <span *ngSwitchDefault class="badge">/// Display 10.00 USD</span>
</td>

Any advice on how I can add the ".00" based on the currency?

Answer №2

To achieve your desired outcome, you can make a slight modification to your current method:

<td *ngIf='transaction.currency=="JPY"'>{{transaction.amount|number:'1.0-0'}} {{transaction.currency}}</td>

<td *ngIf='transaction.currency!=="JPY"'>{{transaction.amount|number:'1.2-2'}} {{transaction.currency}}</td>

The first pipe "|number:'1.2-2'" will format the amount as a number with 2 decimal places, while the text displayed is taken from your transaction object.

Another suggested improvement is:

<td>{{ transaction.amount | currency:transaction.currency:'' }} {{ transaction.currency }}</td>

If you prefer the currency code on the left side, you can use:

<td>{{ transaction.amount | currency:transaction.currency:'code' }}</td>

Answer №3

To utilize the currency pipe, you can implement it in the following way:

<td [ngSwitch]="transaction.currency">
     <span *ngSwitchCase="'JPY'">
        {{ transaction.amount | currency:'JPY':'symbol':'1.0-0'}}
     </span>
     <span *ngSwitchCase="'USD'">
        {{ transaction.amount | currency:'USD':'symbol':'1.2-2'}}
     </span>
     <span *ngSwitchDefault class="badge">
        {{ transaction.amount | currency:'USD':'symbol':'1.2-2'}}
     </span>
</td>

The final parameter of the currency pipe specifies the format for digits as shown below:

{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}

For example, the US currency will display a minimum of one integer digit and a maximum of two decimal digits, while the Japanese currency will not show any decimal places.

Answer №4

Consider implementing the currency filter:

<td>{{transaction.amount | currency: transaction.currency}}</td>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to dynamically updating image URLs based on color selection in Angular when handling ngFor loop

component.ts // necessary imports have been included ngOnInit() { this.list = { 'eatList': [{ 'class': 'Fruits', 'color': ['Red', 'White', 'Black& ...

Guide to selecting a specific element in a loaded template depending on an *ngIf statement

I'm having trouble accessing an element in my template using a template variable as it keeps returning as undefined. At a specific route in my application, my component makes a call to fetch some data within the forkJoin method from rxjs. Due to the ...

Angular Protectors: Stop unauthorized access to a URL while allowing authorized refresh

I've developed a Protection feature that blocks users from directly accessing a specific URL, and it's functioning correctly. However, the issue arises when I try to refresh the page as the Protection feature ends up redirecting me. Below is th ...

Event that occurs when modifying a user's Firebase Authentication details

Monitoring User Actions with Firebase Authentication Within my application built using Angular, Node.js, and Firebase, I am seeking a method to track user events such as additions, modifications, and deletions. Is there a mechanism to recognize when a us ...

Typescript and RxJS: Resolving Incompatibility Issues

In my development setup, I work with two repositories known as web-common and A-frontend. Typically, I use npm link web-common from within A-frontend. Both repositories share various dependencies such as React, Typescript, Google Maps, MobX, etc. Up until ...

What is the best way to retrieve the filepath of a module that is lazily loaded

Here are the routes that I have set up: const routes: Routes = [ { path: 'path1', loadChildren: () => import('./child/child.module').then(r => r.ChildModule), }, { path: 'path2', loadChildren: () =& ...

What are the differences between Modules and Typings in Typescript?

I have been searching far and wide for information on the variances between modules and typings in TypeScript, but I'm still struggling to grasp the concept. As a newcomer to TypeScript, could someone please provide a concise explanation of these diff ...

Navigating with optional parameters appended to the query string

I am faced with a situation where a search can only be performed based on one value at a time (id, age, or sex). If I utilize the following route where only the id is passed, this.router.navigate(['details'], { queryParams: { ...

typescriptIs there a more efficient approach to typing optional values without a default value in

In my React application with TypeScript, I am using the following code to provide typed props: type ScheduleBoxContentProps = { desc: ReactNode, lottie: LottieProps, } & Partial<{className: string}>; I want the className prop to be optional ...

Utilizing Angular to lazily load multiple routes/paths within a single module

I am currently in the process of setting up multiple horizontal routes (not nested) and grouping them into one lazy-loaded module. However, I am facing a challenge trying to properly match these routes within the lazy-loaded feature module itself. Below ...

What could be causing me to consistently receive a 0 value despite my collection having content stored within it?

How can I retrieve the value of dropVolume and use it in another method after executing my getAllDropsVolumePerDate(date) function? Each time I try to access dropVolume, it returns a value of 0. dropVolume = 0; getAllDropsVolumePerDate(date: string) { ...

Setting up the foundations in Angular 4

Using WebStorm 2017.1.4, I am working on creating the front end of my J2EE application. Within this project, I have two components: "about" and "contacts". In the latter component, my goal is to include all contacts from a MySQL database. However, I encoun ...

Utilize decorators for enhancing interface properties with metadata information

Can decorators be utilized to add custom information to specific properties within an interface? An example can help clarify this: Interface for App state: export interface AppState { @persist userData: UserData, @persist selectedCompany: UserCo ...

Invoke a method in a child component from its parent component

Is there a way to trigger a function on a child component from the parent component? In my scenario, I have a ModalComponent (parent) and a MessageComponent (child), and I need them to communicate. In Angular 1, this was achievable through a shared service ...

Filtering JSON array data in Typescript can help streamline and optimize data

Recently diving into Angular, I am facing a challenge with filtering data from a JSON array. My goal is to display names of items whose id is less than 100. The code snippet below, however, is not producing the desired result. list : any; getOptionList(){ ...

Angular 2 form with ng2-bootstrap modal component reset functionality

I have implemented ng2-bs3-modal in my Angular 2 application. I am now looking for a way to clear all form fields when the close button is clicked. Can anyone suggest the best and easiest way to achieve this? html <button type="button" class="btn-u ...

What is the reason that specifying the type of function parameters does not result in conversion being

Seeking clarity here. I have a TypeScript function (using version 1.7) that is linked to the change event of a select dropdown in HTML: toggleWorker(stsID: number): void { // get status object by selected status ID let cStatus: Status = this.s ...

The parameter 'response' is assumed to be of type 'any' and is not explicitly declared

In my attempt to send data from an angular HTML page to MVC core and receive a response, I encountered an error when trying to use the subscribe method: Parameter 'res' implicitly has an 'any' type. Below is the code snippet: import ...

How to send form group in Angular when the enter key is pressed

When I press the submit button on a form, it sends a request to the database to filter data in a grid. However, I also want the form to submit when the enter key is pressed. HTML <form [formGroup]="bmForm" (keyup.enter)="onSearchClic ...

Is VSCode disregarding tsconfig.json and compiling individual files, causing misleading error messages to appear?

After updating typescript, angular, and all the libraries in my project, I encountered a new issue that was not present before. Although I successfully ensured that my code builds without any errors or warnings from the command line, Visual Studio Code sta ...