npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

safe-currency

v1.0.3

Published

currency tool

Downloads

8

Readme

safe-currency API

初始化

自定义配置,默认以人民币为准,也可以根据设置覆盖。

import currency from 'safe-currency'
new currency(value, opts)=> currencyObj
type valueType = {number|string|JSBD|BigInt}
defaultOpts=
{
symbol: '¥',
separator: ',',
decimal: '.',
precision: 2,
pattern: '!#',
format:null,
strict: false,
showTail0:false,
fromCents: false
}

symbol 默认: "¥"

货币符号

new currency(1.23).format(); // => "¥ 1.23"

separator 默认: ","

千分位分隔符

new currency(1234.56, { separator: "," }).format(); // => "¥ 1,234.56"
new currency(1234.56, { separator: " " }).format(); // => "¥ 1 234.56"

decimal 默认: "."

小数点符号

new currency(1.23, { decimal: "." }).format(); // => "¥ 1.23"
new currency(1.23, { decimal: "," }).format(); // => "¥ 1,23"

precision 默认: 2

精度

new currency(1.234, { precision: 2 }); // => "1.23"
new currency(1.234, { precision: 3 }); // => "1.234"

pattern 默认: !#

!指代金额,#指代符号

new currency(1.23, {
  pattern: `# !`,
}).format(); // => "1.23 $"

format 默认: null

自定义 format 函数

function format(currency, options) {
  return `${currency.yuan()}.${currency.cents()}`;
}
new currency(1234.56, { format: format }).format(); // => "1234.56"

strict 默认: false

错误输入是否报错

new currency("12 哈哈.2"); // 12.2
new currency(undefined); // 0
new currency("12 哈哈.2", { errorOnInvalid: true }); // throws an error
new currency(undefined, { errorOnInvalid: true }); // throws an error

showTail0 默认: true

是否显示小数点后末尾零

new currency("123.00").format(); // =>'¥ 123.00'
new currency("123.00", { showTail0: false }).format(); // =>'¥ 123'

fromCents 默认: false

以分为单位创建 currency(运算后不继承)

new currency("123").format(); // =>'¥ 123.00'
new currency("123", { fromCents: true }).format(); // =>'¥ 1.23'

属性

JSBDVal

currency.JSBDVal 返回原始的 JSBD 类型

new currency(123.45).JSBDVal; // => JSBD 对象

value

currency.value 返回字符串表示

new currency(123.45).value; // => '123.45'

方法

add

currency.add(value);

支持输入 string、number、currency、JSBD、BigInt,支持链式调用,运算结果继承配置

new currency(123.45).add(0.01).toString(); // => "123.46"

subtract

currency.subtract(value);

支持输入 string、number、currency、JSBD、BigInt,支持链式调用,运算结果继承配置

new currency(123.45).subtract(0.01).toString(); // => "123.44"

multiply

currency.multiply(number);

支持输入 string、number、currency、JSBD、BigInt,支持链式调用,运算结果继承配置

new currency(123.45).multiply(2).toString(); // => "246.90"

divide

currency.divide(number);

支持输入 string、number、currency、JSBD、BigInt,支持链式调用,运算结果继承配置

new currency(123.45).divide(2).toString(); // => "61.73"

distribute

currency.distribute(string | number | JSBD | BigInt);

不可被整除的场景,支持输入 string、number、JSBD、BigInt。(仅整数)

new currency(1).distribute(3); // => [0.34, 0.33, 0.33]
new currency(12.35).distribute(3); // => [4.12, 4.12, 4.11]
new currency(12.0).distribute(3); // => [4.00, 4.00, 4.00]

compareTo

currency.compareTo(number);

比较大小,支持输入 string、number、currency、JSBD、BigInt,返回-1,0,1(可用于排序)

new currency(1).compareTo(currency(2)); // =>-1

chineseNumber

currency.chineseNumber();

转中文数字

new currency(12345.67).chineseNumber(); // =>"壹万贰仟叁佰肆拾伍元陆角柒分"

format

currency.format(options);

格式化,支持自定义配置,支持自动推断

new currency(1000.0).format(); // => "¥ 1,000.00"
new currency(1000.0, { symbol: "¥", precision: 3 }).format(); // => "¥ 1,000.000"
new currency(1000.0).format({ symbol: "¥", precision: 3 }); // => "¥ 1,000.000"
new currency(1000.0).format({ formatter: () => "烫烫" }); // => "烫烫"
new currency("£1234.500").format(); // =>'£1,234.500' (自动推断货币为 £, 精度 3 位)

toString

currency.toString();

返回以元为单位的表示( 同 yuan() )

new currency(123.45).toString(); // => 123.45
new currency("0.99").toString(); // => 0.99
new currency("1").toString(); // => 1

yuan

currency.yuan() 返回以元为单位的表示,超出 2 位精度四舍五入

new currency("0.99").yuan(); // => 0.99
new currency("1").yuan(); // => 1
new currency(123.45).yuan(); // => 123.45
new currency(123.459).yuan(); // => 123.46

cents

currency.cents() 返回以分为单位的表示,超出 0 位精度四舍五入

new currency("0.99").cents(); // => 99
new currency("1").cents(); // => 100
new currency(123.45).cents(); // => 12345
new currency(123.459).cents(); // => 12346