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

@dawenci/decimal

v0.2.0

Published

big decimal

Downloads

2

Readme

@dawenci/decimal

API

Decimal class

Type:

enum RoundingMode {
  Up = 0,
  Down = 1,
  Ceiling = 2,
  Floor = 3,
  HalfUp = 4,
  HalfDown = 5,
  HalfEven = 6,
  HalfCeiling = 7,
  HalfFloor = 8,
}

class Decimal {
  /**
   * Returns a copy of this Decimal object.
   */
  clone(): Decimal

  /**
   * Integer.
   */
  isInt(): boolean

  /**
   * A **SafeInt** is an integer between `Number.MIN_SAFE_INTEGER` and `Number.MAX_SAFE_INTEGER`.
   */
  isSafeInt(): boolean

  /**
   * Negative.
   */
  isNeg(): boolean

  /**
   * NaN.
   */
  isNaN(): boolean

  /**
   * +/-Infinity.
   */
  isInfinity(): boolean

  /**
   * Equals to `0`.
   */
  isZero(): boolean

  /**
   *  Equals to `1`.
   */
  isOne(): boolean

  /**
   * To JavaScript number.
   */
  toNumber(): number

  /**
   * Returns a string representing the value of this Decimal.
   */
  toString(): string

  /**
   * Returns a string representing the value of this Decimal.
   */
  toPrecision(significantDigits?: number, roundingMode?: RoundingMode): string
}

make instance

function make(any: string | number): Decimal

example:

import { make } from '@dawenci/num'

;[
  make(0),
  make(1),
  make(3.14),
  make(NaN),
  make(Infinity),
].map(num => num.toString())
// return: ['0', '1', '3.14', 'NaN', 'Infinity']

make('3.14') // 3.14
make('314e-2') // 3.14
make('infinity') // Infinity
make('NaN') // NaN

method

Decimal.prototype.toNumber

type

toNumber(): number

get javaScript number value.

example

make(3.14).toNumber() // 3.14
make(0).toNumber() // 0
make(-0).toNumber() // -0
make(NaN).toNumber() // NaN
make(Infinity).toNumber() // Infinity
make(-Infinity).toNumber() // -Infinity

Decimal.prototype.toString

type

toString(): string

example

make(3.14).toString() // '3.14'
make(0).toString() // '0'
make(-0).toString() // '-0'
make(NaN).toString() // 'NaN'
make(Infinity).toNumber() // 'Infinity'
make(-Infinity).toNumber() // '-Infinity'

Decimal.prototype.toPrecision

type

toPrecision(significantDigits?: number, roundingMode?: RoundingMode): string

example

make(3).toPrecision(3) // 3.00
make(3.3).toPrecision(3) // '3.30'
make(3333).toPrecision(3) // '3330'

Decimal.prototype.setPrecision

type

setPrecision(): Decimal

Pipeable API

type

type Pipeable = (decimal: Decimal) => Decimal
type Pipe = (any: any, ...fns: Pipeable[]) => Decimal

const pipe: Pipe

example

import { pipe, add, sub, mul, div } from '@dawenci/num'

pipe(
  1,       // 1
  add(10), // 11
  sub(5),  // 6
  mul(3),  // 18
  div(2)   // 9
).toString()
// output: '9'

pipeable functions

add

pipe(a, add(b)) means a + b.

example

pipe(0.1, add(0.2)).toNumber() // 0.3
sub

pipe(a, sub(b)) means a - b

example

pipe(3, sub(2)).toNumber() // 1
mul

pipe(a, mul(b)) means a * b

example

pipe(3, mul(2)).toNumber() // 6
div

pipe(a, div(b)) means a / b

example

pipe(3, div(2)).toNumber() // 1.5
mod

pipe(a, mod(b)) means a % b

example

pipe(3, mod(2)).toNumber() // 1

neg

Toogle positive and negative symbols.

example

pipe(3.14, neg).toNumber() // -3.14
pipe(-3.14, neg).toNumber() // 3.14
tap

type

tap(fn: (n: Decimal) => void)

example

pipe(3, n => console.log(n.toNumber())) // print 3

change log

v0.2.0

  • 模块导出方式变更,新增导出 fn 下的函数(在 fn 命名空间下)
  • 增加 Setting 模块,可以配置全局的 precision、rounding 模式、转换指数计数法的阈值等。
  • make 函数支持更多输入
  • pipeable 相关类型调整
  • 函数 fn/add、fn/sub、fn/mul、fn/div、fn/mod 等返回 Decimal 实例的函数,会对结果应用 precision、rounding
  • 函数 fn/div 和 pipeable/div 语义、参数重新设计
  • 函数 pipeable/precision 新增 roundingMode 参数
  • 新函数 fn/abs 和 pipeable/abs
  • 新函数 fn/pi,用来生成指定精度的 π
  • 新函数 pipeable/cmp
  • 新函数 pipealbe/neg
  • 函数 pipeable/sub、pipeable/div、pipeable/mod 支持传入占位符交换两个二元运算操作数的顺序
  • Decimal.prototype.toPrecision 方法重新设计(语义更新)
  • Decimal.prototype.toString 方法重新设计(语义更新)
  • Decimal.prototype.toggleNeg 方法移除
  • Decimal.prototype.setNeg 方法移除