Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ssense/number-utils

Package Overview
Dependencies
Maintainers
24
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ssense/number-utils

Number utilities

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
437
decreased by-2.02%
Maintainers
24
Weekly downloads
 
Created
Source

@ssense/number-utils

Usage

Decimal

The type allows to explicitely define precision in contracts/interfaces.

import { Decimal } from '@ssense/number-utils';

export interface OrderTaxes {
    code: string;
    rate: Decimal<6>;
    totalAmount: Decimal<2>;
    details: {
        rate: Decimal<6>;
        name: string;
        amount: Decimal<2>;
    }[];
}

ToDecimal

Why use this function: Naive implementations like parseFloat(num.toFixed(2)) will lead to rounding inconsistencies. eg. parseFloat(436.905.toFixed(2)) = 436.90 instead of 436.91

ToDecimal rounds precisely by acounting for floating point arithmetic inconsistencies. It returns Decimal.

import { NumberUtils } from '@ssense/number-utils';

const subtotal = 75;
const taxRate = 0.14975;
const total = subtotal * (1 + taxRate); // 89.8125

// We cant charge less than 1 cent, hence, the total has to be rounded
const displayedTotal = NumberUtils.toDecimal<2>(total, 2); // 89.81

If interested in learning more on how this works:

  • This guide covers Floating-Point Arithmetic
  • This stackoverflow answer shows concreate examples

FAQs

Package last updated on 07 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc