Created
April 2, 2019 13:39
-
-
Save Sidd27/3b67d9d7adbbcbc6c7b782644f00a248 to your computer and use it in GitHub Desktop.
Indian Currency Pipe for Angular
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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