Code Monkey home page Code Monkey logo

dnum's Introduction

dnum's People

Contributors

bpierre avatar dependabot[bot] avatar savikko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

dnum's Issues

Add option to disable commifying in `dn.format` function

Thedn.format function currently always commifies numbers by default. It would be beneficial to have an option to turn this behavior off. This would provide greater flexibility in formatting number strings and improve the interoperability between dn.from and dn.format.

import * as dn from "dnum";

let b = dn.from("870983127.93887"); // dn.from() can parse strings, numbers, bigint and more

console.log(
  dn.format(b, 2), // Current behavior: "870,983,127.94"
  dn.format(b, { digits: 2, commify: false }), // Desired behavior: "870983127.94"
);

Round to floor when div?

format([49998805n, 9], { digits: 4 } );
// return 0.05
// expected 0.0499

UX issue with normal rounding case:
The user use a little bit of gas token to do a transaction, but the displayed amount of balance still same because formatting is using normal round.

Change divideAndRound to floor div implementation will fix this.
What do you think?

No trailing zero when there is no fraction?

const a = dn.from("5", 2);
dn.format(a, { digits: 2, trailingZeros: true })
// Expected 5.00
// Result 5

I was expecting number toFixed kinda behavior.
Shouldn't it also show trailing zero for consistency?
or is it intended?

const a = dn.from("5", 4);
const b = dn.from("5.1", 4);
const c = dn.from("5.12", 4);
const d = dn.from("5.123", 4);

console.log(dn.format(a, { digits: 4, trailingZeros: true })); // 5
console.log(dn.format(b, { digits: 4, trailingZeros: true })); // 5.1000
console.log(dn.format(c, { digits: 4, trailingZeros: true })); // 5.1200
console.log(dn.format(d, { digits: 4, trailingZeros: true })); // 5.1230

dnum.from Incorrect number from String() converting to scientific notation

in the from method
String(value) converts expoents greater than 21 or less than -6 to scientific notation, which makes it not pass the regex in the next line

I was trying something like multiply(price, 10 ** -8, 18)
It's not a big issue since divide(price, 10 ** Math.abs(expo), 18) should do the same

a possible solution could be something like this, I'm not sure of the implications or if this should be handled in the lib, if you want I can look further and open a pr

  value = String(value);
  
  if (value.includes('e')) value = Number(value).toFixed(20)
  // or value = typeof value === 'number' ? value.toFixed(20) : String(value)

  if (!value.match(NUM_RE)) {
    throw new Error(`Incorrect number: ${value}`);
  }

It took some time today to figure what I was doing wrong here haha

Number.toString

Scientific notation is used if the radix is 10 and the number's magnitude (ignoring sign) is greater than or equal to 10^21 or less than 10-6

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.