I know this question has been asked before but there are a few issues with the answers that have been given that I am trying to fix.
Here is one I found with the following answer:
function calc(number) {
var num = number;
var with2Decimals = num.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0];
return with2Decimals
}
Here is another one I found that works in the same way that I like a little better:
function decimalFix(numToBeTruncated, numOfDecimals) {
var theNumber = numToBeTruncated.toString();
var pointIndex = theNumber.indexOf('.');
return +(theNumber.slice(0, pointIndex > -1 ? ++numOfDecimals + pointIndex : undefined));
}
The issue:
For the most part they work nicely except for extremely large decimal numbers. For example, the number 0.00000001990202020291
reads as 1.99020202029e-8 in javascript so instead of giving me 0.000000019
if I wanted 9 decimal places in it would give me 1.99020202 which is extremely off base.
Why??????
A lot of people have been really upset that I / other people would even want to tackle this issue because it seems unorthodox to them. It mostly has to do with currency and not loosing fractions of the value if I'm only allowed to be so exact in my calculations. For example there are times where I need to calculate micro transactions up to 6-8 decimal places but I'm loosing or gaining money in the process if I choose to round.
I appreciate any help or guidance someone can bring to this issue.
Expected results:
decimalFix(0.0000000199020202029,9) // 0.000000019