Giter Club home page Giter Club logo

babel-plugin-aoto-decimal-js's Introduction

babel-plugin-auto-decimal-js

automatically help you deal with decimal. solve the floating point problem

NPM version Build Status


Why

  • solve the tedious decimal.js code
  • can solve the floating point problem like this
// problem
Decimal(0.57 * 100).toNumber() // 56.999999999

// use this plugin to translate
Decimal(0.57).mul(100).toNumber() // 57

install

$ yarn add decimal.js
$ yarn add babel-plugin-auto-decimal-js --dev

Or

$ npm i decimal.js --save
$ npm i babel-plugin-auto-decimal-js --dev

Usage

// .babelrc
{
  "plugins": [
    "auto-decimal-js"
  ],
  "presets": [
    "react-app"
  ]
}

Example

Transforms

import Decimal from 'decimal.js';

const getNum = () => {
  return 10;
}

const test1 = Decimal(0.57 * 100);
const test2 = new Decimtal(10 * 20 * 30 * '50' * getNum());
const test3 = new Decimal(10 * 20 * 30 - '50' * getNum());
const test4 = new Decimal(10 - 10 * 30);
const test5 = new Decimal((10 - 10 * 30) * 30 * (20 - 10)).toFixed(3, 1);
const test6 = new Decimal(10 / 2 * 3 - 5 * 20 / 3.3);
const test7 = new Decimal(20 % 3 * 11);
const test8 = new Decimal(30 * 2 ** 2);

roughly to

import Decimal from 'decimal.js';

const getNum = () => {
  return 10;
};

const test1 = Decimal(0.57).mul(100);
const test2 = new Decimtal(10 * 20 * 30 * '50' * getNum());
const test3 = Decimal(10).mul(20).mul(30).sub(Decimal('50').mul(getNum()));
const test4 = Decimal(10).sub(Decimal(10).mul(30));
const test5 = Decimal(10).sub(Decimal(10).mul(30)).mul(30).mul(Decimal(20).sub(10)).toFixed(3, 1);
const test6 = Decimal(10).div(2).mul(3).sub(Decimal(5).mul(20).div(3.3));
const test7 = Decimal(20).mod(3).mul(11);
const test8 = Decimal(30).mul(Decimal(2).pow(2));

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.