@orochi-network/utilities
TypeScript icon, indicating that this package has built-in type declarations

0.2.20 • Public • Published

@orochi-network/utilities

An one for all utilities library, this contain all utilities that need for Smart Contract test, Oracle, Orand.... This package is working for both cjs and esm and It's supposed to work on node and browser. It it isn't working please let us know.

Installation:

yarn add @orochi-network/utilities

FixedFloat: A JavaScript Fixed-Point Number Library

This library provides a FixedFloat class for representing and manipulating fixed-point decimal numbers in JavaScript. Fixed-point numbers offer an alternative to floating-point numbers, ensuring consistent decimal precision for financial calculations and other scenarios where exact decimal representation is crucial.

Features:

  • Represents numbers with a fixed number of decimal places.
  • Supports basic arithmetic operations (addition, subtraction, multiplication, division).
  • Handles decimal point scaling for consistent calculations.
  • Provides methods for formatting numbers in decimal and pretty-printed formats.

Usage:

  1. Import the FixedFloat class:
import { FixedFloat } from '@orochi-network/utilities';
  1. Create FixedFloat instances:
  • From a number or string, .from(): This method is simply tell the FixedFloat to take the given value and convert it for a given decimals. For example FixedFloat.from(12.3456, 2) will be convert to { basedValue: 1234n, decimals:2 }. It's represent for 12.34. Apparently, basedValue = givenValue * 10^decimals.
const fixed1: FixedFloat = FixedFloat.from(12.3456); // Represents 12.3456 in 4 decimal places
const fixed2: FixedFloat = FixedFloat.from('3.14159'); // Represents 3.1415 in 5 decimal places
  • From a IFixedFloat, .fromFixedFloat(): This method create a new instance of FixedFloat from its basedValue and decimals. If decimals < 0, basedValue = basedValue * 10^(|decimals|), otherwise it will take basedValue and decimals as input and created new instance of FixedFloat
const fixed3: FixedFloat = FixedFloat.fromFixedFloat({ basedValue: 123456n, decimals: 4 }); // Represents 12.3456
  1. Perform calculations:
const value: FixedFloat = fixed1.add(fixed2).sub(fixed1);
  1. Format the output:
const decimalString: string = FixedFloat.fromFixedFloat({ basedValue: 1234n, decimals: 2 }).toFixed(); // Output: "12.34"
const prettyString: string = FixedFloat.from(1234.5672).pretty(); // Output: "1,234.5672" (locale-specific formatting)

Example:

import FixedFloat from '@orochi-network/utilities';

const price: FixedFloat = FixedFloat.from(12.99, 2);
const quantity: FixedFloat = FixedFloat.fromFixedFloat({ basedValue: 2n, decimals: 0 });

const total: FixedFloat = price.mul(quantity);

console.log(`Total price: ${total.toFixed(2)}`); // Output: Total price: 25.98

Additional Notes:

  • The decimals parameter during creation defines the number of decimal places for the FixedFloat instance.
  • The toFixed and pretty methods allow for customized decimal formatting.
  • The library performs validation on input values and throws errors for invalid operations like division by zero.

Readme

Keywords

Package Sidebar

Install

npm i @orochi-network/utilities

Weekly Downloads

60

Version

0.2.20

License

Apache-2.0

Unpacked Size

56.6 kB

Total Files

27

Last publish

Collaborators

  • chiro-hiro