Utils (@pim.sk/utils)

numbers

Utility functions for working with numbers: string-to-number conversion, percentage calculation, byte formatting, financial rounding, and proportional recalculation.
import { cislo, percento, percPodiel, formatBytes, cenaNa5Centov, cenaNa10Centov, ratio } from '@pim.sk/utils/numbers.mjs'

Usage

cislo( value, [decimals = 3] )

Converts a string or number to a float with the given number of decimal places. Strips spaces, currency symbols and replaces comma with dot before parsing.

cislo( "1 234,56" )       // → 1234.56  (space + comma)
cislo( "€ 99.5" )         // → 99.5     (€ prefix)
cislo( "9,5 €" )          // → 9.5      (€ suffix + comma)
cislo( "12.5 kg" )        // → 12.5     (unit suffix)
cislo( 3.14159, 2 )       // → 3.14     (round to 2 decimals)
cislo( 3.14159, 0 )       // → 3        (round to integer)
cislo( "1 234,56" )   // → 1234.56
cislo( "€ 99.5" )     // → 99.5
cislo( "9,5 €" )      // → 9.5
cislo( 3.14159, 2 )   // → 3.14

percento( zaklad, podiel ) | percPodiel( zaklad, percento )

percento() — what percentage is podiel of zaklad. percPodiel() — what is X percent of zaklad. Both accept optional fraction (default 3).

// How many % is 50 of 200?
percento( 200, 50 )           // → 25

// What is 25% of 200?
percPodiel( 200, 25 )         // → 50

// Custom decimal precision
percento( 333, 1, 6 )         // → 0.3003
percento( 333, 1, 1 )         // → 0.3
percento( 200, 50 )       // → 25      (50 is 25% of 200)
percPodiel( 200, 25 )     // → 50      (25% of 200 is 50)
percento( 333, 1, 6 )     // → 0.3003  (custom fraction)

formatBytes( bytes, [decimals = 2] )

Converts a byte count to a human-readable string. Automatically picks the right unit (KB, MB, GB…).

formatBytes( 0 )              // → "0 Bytes"
formatBytes( 1024 )           // → "1 KB"
formatBytes( 1048576 )        // → "1 MB"
formatBytes( 1073741824 )     // → "1 GB"
formatBytes( 1500000, 0 )     // → "1 MB"   (0 decimals)
formatBytes( 1500000, 4 )     // → "1.4305 MB"
formatBytes( 1024 )           // → "1 KB"
formatBytes( 1048576 )        // → "1 MB"
formatBytes( 1500000, 0 )     // → "1 MB"

cenaNa5Centov( cena ) | cenaNa10Centov( cena )

Financial rounding to the nearest 5 or 10 cents. Useful for cash transactions where smallest coin is 5c or 10c.

cenaNa5Centov( 1.23 )   // → 1.25
cenaNa5Centov( 1.22 )   // → 1.20
cenaNa5Centov( 1.27 )   // → 1.25

cenaNa10Centov( 1.24 )  // → 1.20
cenaNa10Centov( 1.25 )  // → 1.30
cenaNa10Centov( 1.35 )  // → 1.40
cenaNa5Centov( 1.23 )   // → 1.25
cenaNa5Centov( 1.22 )   // → 1.20
cenaNa10Centov( 1.25 )  // → 1.30

new ratio( nums, [options] )

Proportional recalculation of an array of numbers. Fix one value — the rest are redistributed to maintain the original sum. Supports min, max and decimal precision constraints.

options = {
    dec: 2,    // decimal precision (default 2)
    min: null, // minimum value for recalculated numbers
    max: null, // maximum value
    fix: null, // pre-fix specific indexes: [ true, false, false, true ]
}

const rat = new ratio( [10, 10, 10, 10] )
rat.num( 2, 15 )         // fix index 2 to 15
rat.nums                 // → [ 8.33, 8.33, 15, 8.33 ]

rat.num( 0, 20 )         // fix index 0 to 20 (index 2 stays fixed too)
rat.nums                 // → [ 20, 2.5, 15, 2.5 ]

rat.reset()              // unfix all — nums stay, fix flags cleared
const rat = new ratio( [10, 10, 10, 10] )
rat.num( 2, 15 )
rat.nums   // → [ 8.33, 8.33, 15, 8.33 ]

// with options
const rat2 = new ratio( [10, 10, 10, 10], { dec: 1, min: 5 } )
rat2.num( 2, 15 )
rat2.nums  // → [ 8.3, 8.3, 15, 8.3 ]

// pre-fixed indexes
const rat3 = new ratio( [10, 10, 10, 10], { fix: [true, false, false, true] } )
rat3.num( 2, 15 )
rat3.nums  // → [ 10, 7.5, 15, 10 ]
v 1.1.2