Angular digit organization - adding a space between each grouping of a thousand

In the process of formatting numbers, I am encountering different scenarios - some are directly defined in the HTML template, while others are fetched using @ViewChild textContent in TS. The desired format is to display numbers like this: 1 213.00.

Specifically, after each thousand, there should be an empty space.

I am looking for a solution that can be applied in both HTML and TS. How can I achieve this flexibility?

Your assistance is greatly appreciated!

Answer №1

Develop a personalized pipe:

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

@Pipe({
  name: 'modifyComma'
})
export class ModifyCommaPipe implements PipeTransform {

  transform(value: string | null): string {
    if (value !== undefined && value !== null) {
      return value.replace(/,/g, " ");
    } else {
      return "";
    }
  }
}

Next, use the pipe in your template:

{{ decimalValue | number | modifyComma }}

Here, the number is Angular's predefined decimal pipe.

In your component:

import { DecimalPipe } from '@angular/common';
import { ModifyCommaPipe } from 'your-custom-pipe-path';

constructor(private decimalPipe: DecimalPipe, private modifyCommaPipe: ModifyCommaPipe) {
}
...
decimalValue = 1234567;
this.modifyCommaPipe.transform(this.decimalPipe.transform(this.decimalValue))

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

Angular 6 - Accessing grandparent methods in grandchild components

I am in need of running the functions of the grandparent component: import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.cs ...

Creating multiple ngApps in Angular 4, 5, or 6

In our organization, we are considering a transition from Struts/Servlets to Angular 6 with a complete rewrite. However, since we have limited experience with Angular technology, I have some concerns. I am contemplating creating a shared Angular micro-s ...

typescript: textual depiction of a generic type T

I have a requirement to develop a method that is capable of handling generic data types, and I need to incorporate the type information into the method Our API requires passing the entity name as a parameter: http://server/api/fetch/Entity/123 It's ...

Creating unique angular index.html files

Upon loading the application, the server verifies if the user has permission to access it. If not, I want to direct them to an error page. The snippet below is taken from index.html: <app-root></app-root> In the main.ts file: platformB ...

Removing a particular item from an Observable of arrays containing any type

My application has an Observable that contains an array of places: places: Observable<Array<any>>; In the template, I am using the async pipe to iterate over the array: <tr *ngFor="let place of places | async"> ... </tr> After ...

Reactive form enabled for Angular control

In order to meet the required format for a social security number input field using reactive forms, it must adhere to the following conditions: The format should be ###-##-####. Limited to 9 digits without including the dashes (11 characters with dashes) ...

Creating a merged object from a split string array in TypeScript

I possess an array containing objects structured as follows; const arr1 = [ {"name": "System.Level" }, {"name": "System.Status" }, {"name": "System.Status:*" }, {"name": "System.Status:Rejected" }, {"name": "System.Status:Updated" } ] My object ...

Issue with Angular App: Bootstrap navbar not displaying on smaller screens

I am working on an Angular app (using Angular 11.2.4 with Bootstrap 4.5.3) and facing an issue where the navbar is not rendering correctly on screens smaller than ~580 pixels wide. Even when I click the toggler to 'expand' the collapse region, n ...

Combine two arrays of data sources

mergeThreads() { const userId = this.auth.getUser().uid; const buyerThreads$ = this.afs.collection('threads', ref => ref.where('buyerId', '==', userId)).valueChanges(); const sellerThreads$ = this.afs.collection ...

How should I handle a situation where the placeholder type is set as a string, but the actual value is a number?

Within my TSX file, I have a component that serves as an input. import { InputHTMLAttributes } from "react"; interface InputProps extends InputHTMLAttributes<HTMLInputElement> { placeholder: string, label?: string, } export const ...

I am interested in utilizing Template literal types to symbolize placeholders

Currently, I am in the process of converting existing javascript files into typescript for my business needs. Below is an example object structure: [ { // Sample column names givenName, familyName, and picture are provided as examples. "giv ...

Using npm and systemjs as a build tool in an Angular 2 app (RC 5): A step-by-step guide

While I am well-versed in using gulp and systemjs for bundling applications (such as utilizing compression, bundling, exporting to build folder, etc), my boss now requires just npm and systemjs to complete the task. I am unsure of how to create a custom sc ...

Is there a method to implement retries for inconsistent tests with jest-puppeteer?

Currently, I am struggling with an issue where there is no built-in method to retry flaky tests in Jest. My tech stack includes Jest + TypeScript + Puppeteer. Has anyone else encountered this problem or have any suggestions for possible solutions? I attem ...

Tips for ensuring the reliability of unit tests when testing an OnPush component in Angular with fixture.detectChanges()

I developed a project where I implemented a Component that fetches data asynchronously from a service and displays it to the user. The Component code is as follows: @Component({ changeDetection: ChangeDetectionStrategy.Default, selector: 'test-co ...

What is the best way to check the API response status in NextJS13?

Currently, I am experimenting with different methods to handle API HTTP status in my NextJS-13 project but so far nothing has been successful. Note: TypeScript is being used in this project. Below is my code snippet with a static 200 API response and the ...

Guide on accessing the afterClosed() method / observable in Angular from a Modal Wrapper Service

Currently, I am in the process of teaching myself coding and Angular by developing a personal app. Within my app, I have created a wrapper service for the Angular Material ModalDialog. It's a mix of Angular and AngularJS that I've been working on ...

"Embedding a JSON object within a script tag in NextJS: A step-by-step

I've been working on configuring values within a Cookiebot initialization script tag in a NextJS project. The documentation suggests that I can set vendor properties by passing in a JSON object inside the script tag, like this (taken from this link): ...

Using ngIf with Promises causes a malfunction

I have extensively tested this issue and I am unable to understand why the code below is not functioning as expected. The problem arises when the @Input variable is received and the user object is fetched from the service, the ngIf directive in the templat ...

Angular and C# working together to automatically round large numbers with decimals

I'm facing an issue with my database where I have a value of 100000000000000.165. When I validate this value using an API tester, I get the expected result. https://i.sstatic.net/93NXp.png However, when I retrieve the value in my Angular app and che ...

Using custom elements in both React and Angular is not supported

After successfully creating a custom element in React using reactToWebComponent, I integrated it into a basic HTML file like this: <body> <custom-tag></custom-tag> <script src="http://localhost:3000/static/js/bundle.js&quo ...