-
I looked through all issues and the docs, but I couldn't find an answer to my question. const config = {
number: "BigNumber",
precision: 79,
};
const math = create({ expDependencies }, config);
const mbn = this.math.bignumber!;
function exp(x: string): string {
const result = mbn(x).exp() as BigNumber;
console.log(result.toString());
}
exp("-1"); The code above logs the following: 0.3678794411714423215955237701614608674458111310317678345078368016974614957448998" Now, I would like to truncate the result to 18 digits after the dot. How can I do that? I looked at the format function but it seems like the maximum numbers of digits permitted is 16. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I found the answer myself. math.js is built on top of decimal.js, which has a toFixed method. It is similar to the vanilla Thus I would rewrite the example like this: function exp(x: string): string {
const result = mbn(x).exp() as BigNumber;
console.log(result.toFixed(18));
} And get this in the console:
|
Beta Was this translation helpful? Give feedback.
-
There is a powerful function https://mathjs.org/docs/reference/functions/format.html Your example would look like: function exp(x: string): string {
const result = mbn(x).exp() as BigNumber;
console.log(math.format(result, { notation: 'fixed', precision: 18 }));
} |
Beta Was this translation helpful? Give feedback.
I found the answer myself. math.js is built on top of decimal.js, which has a toFixed method. It is similar to the vanilla
toFixed
available in JavaScript, but it can be used only with decimal.js instances.Thus I would rewrite the example like this:
And get this in the console: