subtract
Subtracts one number from another with precision handling for financial calculations. Supports both regular numbers and bigint for exact precision.
Implementation
View Source Code
ts
/**
* Subtracts one number from another with precision handling for financial calculations.
* Supports both regular numbers and bigint for exact precision.
*
* @example
* ```ts
* subtract(20, 10); // 10
* subtract(0.3, 0.1); // 0.2 (precision-safe)
* subtract(300n, 100n); // 200n
* ```
*
* @param a - Number to subtract from
* @param b - Number to subtract
* @returns Difference of a and b
*/
export function subtract(a: number, b: number): number;
export function subtract(a: bigint, b: bigint): bigint;
export function subtract(a: number | bigint, b: number | bigint): number | bigint {
if (typeof a === 'bigint' && typeof b === 'bigint') {
return a - b;
}
if (typeof a === 'number' && typeof b === 'number') {
return a - b;
}
throw new TypeError('Both arguments must be of the same type (number or bigint)');
}Features
- Type-Safe: Separate overloads for number and bigint
- Precision: Works with bigint for financial calculations
- Error Handling: Prevents mixing number and bigint types
- Isomorphic: Works in both Browser and Node.js
API
ts
function subtract(a: number, b: number): number;
function subtract(a: bigint, b: bigint): bigint;Parameters
a: Number to subtract from (minuend)b: Number to subtract (subtrahend)
Returns
- Difference of
aminusb(same type as input)
Examples
Basic Number Subtraction
ts
import { subtract } from '@vielzeug/toolkit';
subtract(20, 10); // 10
subtract(100, 30); // 70
subtract(0.3, 0.1); // 0.2 (precision-safe)Negative Results
ts
import { subtract } from '@vielzeug/toolkit';
subtract(10, 20); // -10
subtract(5, 15); // -10BigInt for Financial Precision
ts
import { subtract } from '@vielzeug/toolkit';
// Working with cents
const total = 100000n; // $1,000.00
const discount = 15000n; // $150.00
subtract(total, discount); // 85000n ($850.00)Real-World Example: Refund Calculation
ts
import { subtract } from '@vielzeug/toolkit';
const orderTotal = 45999n; // $459.99
const refundAmount = 12500n; // $125.00
const remaining = subtract(orderTotal, refundAmount);
// 33499n ($334.99)Calculate Change
ts
import { subtract } from '@vielzeug/toolkit';
const amountPaid = 10000n; // $100.00
const price = 8795n; // $87.95
const change = subtract(amountPaid, price);
// 1205n ($12.05)Implementation Notes
- Both parameters must be the same type (number or bigint)
- Throws
TypeErrorif you try to mix number and bigint - For financial calculations, use bigint to store amounts in minor units (cents)
- Handles negative results correctly
- Can result in negative numbers when subtracting larger value from smaller