Git Product home page Git Product logo

monetize's Introduction

monetize

A lightweight library used to format a number to a currency format and parse such values back to numbers.

Installation

npm install @kainiedziela/monetize --save

Usage

import { monetize, demonetize } from "@kainiedziela/monetize"
monetize(1.23) // -> "$1.23"
demonetize("$1.23") // -> 1.23

When using demonetize on a monetized value that had custom options remember to pass those same options to as a param to demonetize, otherwise it may not parse such a value properly. This mainly concerns decimal and negativePattern options.

Options

You can customize the formatting and parsing of monetize and demonetize with an optional options object. These values default to US centric currency values, but they can be overridden based on your locale.

symbol, default: "$"

monetize(1.23) // -> "$1.23"
monetize(1.23, { symbol: '€' }) // -> "€1.23"

groups, default: 3

The number of digits to be grouped into a single grouping.

monetize(1000.23) // -> "$1,000.23"
monetize(1000.23, { groups: 2 }) // -> "$10,00.23"

separator, default: ","

Separator between the number groupings.

monetize(1000.23) // -> "$1,000.23"
monetize(1000.23, { separator: "" }) // -> "$1000.23"
monetize(1000.23, { separator: " " }) // -> "$1 000.23"

decimal, default: "."

monetize(1.23) // -> "$1.23"
monetize(1.23, { decimal: "," }) // -> "$1,23"

precision, default: 2

Number of decimal places to store as the fractional.

monetize(1.23) // -> "$1.23"
monetize(1.23, { precision: 0 }) // -> "$1"
monetize(1.23, { precision: 3 }) // -> "$1.230"

pattern, default: "!#"

Allows you to customize the format pattern using ! as replacement for the currency symbol and # as replacement for the currency amount.

monetize(1.23) // -> "$1.23"
monetize(1.23, { pattern: "# !" }) // -> "1.23 $"
monetize(1.23, { pattern: "<!#>" }) // -> "<$1.23>"
monetize(1.23, { pattern: "#" }) // -> "1.23"

negativePattern, default: "-!#"

Allows you to customize the negative format pattern using ! as replacement for the currency symbol and # as replacement for the currency amount.

monetize(1.23) // -> "$1.23"
monetize(1.23, { negativePattern: "!-#" }) // -> "$-1.23"
monetize(1.23, { negativePattern: "-# !" }) // -> "-1.23 $"
monetize(1.23, { negativePattern: "(!#)" }) // -> "($1.23)"
monetize(1.23, { negativePattern: "-#" }) // -> "-1.23"

monetize's People

Contributors

kainiedziela avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.