I created a custom Angular pipe to manage and display such numbers with space.
The aim is to have a generic method that is able to transform one Input (a number), and transform it into what I want (a formatted number). Angular pipes are made and great to do that.
Here is a sample, you can inspire yourself from it and only take what you want.
I used an Euclidean division to manage any length of a number.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'formatNumber'
})
export class FormatNumberPipe implements PipeTransform {
transform(value: any): string {
if (value) {
// Gets the value as a string
if (typeof value !== 'string') {
value = value.toString();
}
// Delete existing spaces
while ((value as string).indexOf(' ') !== -1) {
value = (value as string).replace(' ', '');
}
// Manage decimal values
let integerPart: string = value;
if (value.indexOf('.') !== -1) {
integerPart = value.slice(0, value.indexOf('.'));
}
if (value.indexOf(',') !== -1) {
integerPart = value.slice(0, value.indexOf(','));
}
let firstSlice = true;
const arrayResults: Array<string> = [];
let finalResult = '';
const divisor = 3;
const dividend: number = integerPart.length;
let remainder = dividend % divisor;
let quotient = (dividend + remainder) / divisor;
if (dividend >= 3) {
do {
if (firstSlice && remainder > 0) {
// Manage numbers with remainders
firstSlice = false;
arrayResults.push(integerPart.slice(0, remainder));
} else {
// Slice each part of the number to an array
firstSlice = false;
arrayResults.push(integerPart.slice(remainder, remainder + divisor));
remainder = remainder + divisor;
quotient--;
}
// Continue dividing the number while there are values
} while (quotient >= 1);
// Concats the sliced parts to build the final number
arrayResults.forEach(part => {
finalResult += `${part} `;
});
// Delete any trailing whitespace
finalResult = finalResult.trim();
return finalResult;
} else {
return value;
}
}
return value;
}
}
Once the pipe is done, you can use it however you want in your app :
Either in your template directly :
<input [ngModel]="value | formatNumber" (ngModelChange)="value = $event">
Or in your code directly using Dependency Injection of the pipe :
this.sommeFormat.totalValue = this.formatNumber.transform(this.somme.totalValue);
Here is the official documentation about Angular pipes : https://angular.io/guide/pipes