Noodle Printing Machine

    @ssense/number-utils
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.1 • Public • Published

    @ssense/number-utils

    Usage

    Decimal

    The type allows to explicitely define precision in contracts/interfaces.

    import { Decimal } from '@ssense/number-utils';
    
    export interface OrderTaxes {
        code: string;
        rate: Decimal<6>;
        totalAmount: Decimal<2>;
        details: {
            rate: Decimal<6>;
            name: string;
            amount: Decimal<2>;
        }[];
    }

    ToDecimal

    Why use this function: Naive implementations like parseFloat(num.toFixed(2)) will lead to rounding inconsistencies. eg. parseFloat(436.905.toFixed(2)) = 436.90 instead of 436.91

    ToDecimal rounds precisely by acounting for floating point arithmetic inconsistencies. It returns Decimal.

    import { NumberUtils } from '@ssense/number-utils';
    
    const subtotal = 75;
    const taxRate = 0.14975;
    const total = subtotal * (1 + taxRate); // 89.8125
    
    // We cant charge less than 1 cent, hence, the total has to be rounded
    const displayedTotal = NumberUtils.toDecimal<2>(total, 2); // 89.81

    If interested in learning more on how this works:

    Keywords

    none

    Install

    npm i @ssense/number-utils

    DownloadsWeekly Downloads

    317

    Version

    1.0.1

    License

    ISC

    Unpacked Size

    3.09 kB

    Total Files

    5

    Last publish

    Collaborators

    • jeegna.patel
    • jamesslomka
    • ssenses-agupta
    • expense_ssense
    • toya.okeke
    • quinnlangille
    • ssense-admin
    • kaiz
    • maxime.servonnet.ssense
    • ephremsaour
    • non-senses
    • sam-nicolai.johnston
    • carlossense
    • npm1
    • vijayrajasekar
    • dimitrios.nicolacopoulos
    • sikkavarun
    • lclemence
    • nodir.fayzullaev
    • alexnorms
    • mahdieft