Skip to content

Instantly share code, notes, and snippets.

@Sidd27
Created April 2, 2019 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sidd27/3b67d9d7adbbcbc6c7b782644f00a248 to your computer and use it in GitHub Desktop.
Save Sidd27/3b67d9d7adbbcbc6c7b782644f00a248 to your computer and use it in GitHub Desktop.
Indian Currency Pipe for Angular
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'indianCurrency'
})
export class IndiaCurrencyPipe implements PipeTransform {
transform(amount: any, args?: any): string {
if (amount !== null && !isNaN(amount)) {
const currencySymbol = '₹ ';
const decimalValue = this.getDecimalValue(amount.toString());
const intergerValue = Math.floor(amount).toString().replace(/(\d)(?=(\d\d)+\d$)/g, '$1,');
return currencySymbol + intergerValue + decimalValue;
}
}
getDecimalValue(amount: string): string {
let decimal = '';
if (amount.indexOf('.') > 0) {
decimal = amount.substring(amount.indexOf('.'), amount.length);
decimal = decimal.length === 2 ? decimal + '0' : decimal.substring(0, 3);
} else {
decimal = '.00';
}
return decimal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment