splitInclusive function

SalesTaxAmounts? splitInclusive(
  1. num gross,
  2. num rate
)

Splits an amount that includes sales tax into its value and tax.

The value is rounded half up to the paisa, and the tax is the rest, so net + tax is always gross. gross is 0 to 1 trillion rupees; rate is a percent, 0 to 100, with at most 2 decimals.

Returns null for an invalid amount or rate. Never throws.

splitInclusive(1180, 18);
// SalesTaxAmounts{net: 1000.0, tax: 180.0, gross: 1180.0, rate: 18.0}

Implementation

SalesTaxAmounts? splitInclusive(num gross, num rate) {
  final grossPaisa = toUnits(gross, 2);
  final bp = _basisPoints(rate);
  if (grossPaisa == null || bp == null) return null;
  final netPaisa = divideHalfUp(
    BigInt.from(grossPaisa) * BigInt.from(10000),
    BigInt.from(10000 + bp),
  );
  return _amounts(netPaisa, grossPaisa - netPaisa, bp);
}