Files
brewman/overlord/src/app/shared/math.service.ts
tanshu cfeef1795d Feature: Allow math expressions on all journal inputs and also round them properly.
Chore:
  Prettied index.html, main.ts and styles.css
  Updated Dependencies
2020-10-07 18:42:22 +05:30

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);
}
}
}