bpierre / dnum Goto Github PK
View Code? Open in Web Editor NEW🧮 Small library for big decimal numbers.
License: MIT License
🧮 Small library for big decimal numbers.
License: MIT License
First of all, thanks for working on this library, it's incredibly useful and well designed.
The docs seem to state that the number of digits is optional for the toNumber
utility:
Defaults to the number of decimals in the Dnum passed to value.
However, TS will complain if/when only the Dnum is passed. This doesn't happen with the options for other utilities (e.g., format
).
I cannot make my unit tests work.
I get this error:
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
If I remove the lines of code that exploit dnum the tests work perfectly.
This is my jest.config.js:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ["<rootDir>/src"],
testMatch: ['**/*.test.(ts|tsx)'],
};
The type definitions in the build use relative import paths that don't include the extensions. This means projects using Node16
or NodeNext
moduleResolution
in their typescript projects won't be able to resolve the types.
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"
);
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
Can you export the Numberish type?
It's used in all the methods that you expose.
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?
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
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.