Nuns Practicing Misanthropy

    js-number-formatter

    1.0.1 • Public • Published

    Number Formatter

    A javascript module that scans through string to remove non-numerals and formats out any found numbers based on configuration passed to it.

    💾 Setup

    You need to have NodeJS installed on your computer before using npm. Open a terminal/command prompt and enter the codes below to install the package:

    npm install js-number-formatter

    🔨 Usage

    // -- Require the module
    const { JS_NumberFormat } = require('js-number-formatter')
     
    const value = '3.142857'
     
    const options = {
      // ... check the parameters section for more info on options
    }
     
    // -- Call function and pass the value to format and options to it or just the value
    JS_NumberFormat(value, options)

    📙 Parameters

    Parameter Description
    value The value to format which can be a number or string
    options
    • op_ReturnZeroIfNoDigit
      true by default
      Returns zero '0' if string contains no digits
    • op_ReturnAbsoluteNumber
      true by default
      Return the absolute number without possible preceeding zeros '0'
    • op_AllowSign
      true by default
      Allows sign (-) if true, removes any present sign if false
    • op_AllowDecimal
      true by default
      If decimal should be allowed or not
    • op_ForceDecimal
      true by default
      Forces decimal even if string contains no decimal points (but only if op_AllowDecimal is true)
    • op_AppendZeroToDecimal
      true by default
      Appends zero '0' to result if just one digit is found after the last decimal point
    • op_DecimalDelimiterChar
      '.' by default
      Character to use im place of the decimal period symbol
    • op_DelimiterChar
      ',' by default
      Thousands separator
    • op_AddSpaceToDelimiter
      false by default
      Thousands character spacing with ' '

    🎨 Examples

    JS_NumberFormat(3892) // Result: 3,892.00
     
    JS_NumberFormat('140hajs7') // Result: 1,407.00
     
    JS_NumberFormat("you can't be serious ...") // Result: 0
     
    JS_NumberFormat("Hello World 20-06-2019", {
      op_AllowSign: false
    }) // Result: 20,062,019.00
     
    JS_NumberFormat('-00034dj^nkjlsd$knls4h%bj.34.5', {
      // -- Returns zero '0' if string contains no digits
      // default: true, [test: '-00034.dj^nkjls.d$knls.4h%bj345' | true: -34.4345 | false: -34.4345]
      op_ReturnZeroIfNoDigit: true,
      // -- Return the absolute number without possible preceeding zeros '0'
      // default: true, [test: '-00034.dj^nkjls.d$knls.4h%bj345' | true: -34.4345 | false: -00,034.4345]
      op_ReturnAbsoluteNumber: true,
      // -- Allows sign (-) if true, removes any present sign if false
      // default: true [test: '-00034.dj^nkjls.d$knls.4h%bj345' | true = -34.4345 | false = 34.4345]
      op_AllowSign: true, 
      // -- If decimal should be allowed or not
      // default: true [test: '-00034.dj^nkjls.d$knls.4h%bj345' | true = -34.4345 | false = -34]
      op_AllowDecimal: true, 
      // -- Forces decimal even if string contains no decimal points (but only if op_AllowDecimal is true)
      // default: true [test: '-00034dj^nkjlsd$knls4h%bj345' | true: -344,345.00 | false: -344,345]
      op_ForceDecimal: true, 
      // -- Appends zero '0' to result if just one digit is found after the last decimal point
      // default: true [test: '-00034dj^nkjlsd$knls4h%bj34.5' | true: -34,434.50 | false: -34,434.5]
      op_AppendZeroToDecimal: true,
      // -- Character to use im place of the decimal period symbol
      // default: '.'
      op_DecimalDelimiterChar: '.',
      // -- Thousands separator
      // default: ','
      op_DelimiterChar: ',',
      // -- Thousands character spacing with ' '
      // default: false [test: '-00034dj^nkjlsd$knls4h%bj.34.5' | true: -34, 434.50 | false: -34,434.50]
      op_AddSpaceToDelimiter: true
    })

    Install

    npm i js-number-formatter

    DownloadsWeekly Downloads

    187

    Version

    1.0.1

    License

    ISC

    Unpacked Size

    8.93 kB

    Total Files

    4

    Last publish

    Collaborators

    • lolusholar