Skip to content

How to truncate up to n digits after the dot? #2328

Answered by PaulRBerg
PaulRBerg asked this question in Q&A
Discussion options

You must be logged in to vote

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:

function exp(x: string): string {
  const result = mbn(x).exp() as BigNumber;
  console.log(result.toFixed(18));
}

And get this in the console:

0.367879441171442322

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by PaulRBerg
Comment options

You must be logged in to vote
2 replies
@PaulRBerg
Comment options

@josdejong
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants