27 lines
646 B
TypeScript
27 lines
646 B
TypeScript
import { Injectable } from '@angular/core';
|
|
import { evaluate, round } from 'mathjs';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class MathService {
|
|
constructor() {}
|
|
|
|
journalAmount(amount: string = '', debit: number): any {
|
|
const val = this.parseAmount(amount, 2);
|
|
if (val < 0) {
|
|
debit *= -1;
|
|
}
|
|
return { debit: debit, amount: Math.abs(val) };
|
|
}
|
|
|
|
parseAmount(amount: string = '', rounding: number = 2): number {
|
|
const cleaned = ('' + amount).replace(new RegExp('(₹[s]*)|(,)|(s)', 'g'), '');
|
|
if (cleaned === '') {
|
|
return 0;
|
|
} else {
|
|
return round(evaluate(cleaned), rounding);
|
|
}
|
|
}
|
|
}
|