Reputation: 31
Why does the following return a fractional component error for exceeding decimals? It only seems to trigger on low balance.
If my balance is 0.1 ether or BNB I don't see an error but if my balance is 0.0001 I would see this error
Error: fractional component exceeds decimals
var account = accounts[0]
var yourBalance = await provider.getBalance(account)
var fixedBalance= ethers.FixedNumber.fromValue(yourbalance , 18);
var stakedAmount = (fixedBalance * 0.50).toString();
var finalOutput = ethers.utils.parseUnits(stakedAmount , 18); //this returns Error: fractional component exceeds decimals
Upvotes: 2
Views: 6058
Reputation: 633
The simplest thing you can do is round the number to the appropriate decimal places before calling ethers.utils.parseUnits
using toFixed
, Math.round or etc.:
const n = '1.48389134738';
const decimals = 6;
ethers.utils.parseUnits(Number(n).toFixed(decimals), decimals);
Upvotes: 0
Reputation: 5661
If USDT supports 6 decimals
and my input is 153.60000000000002
(more than 6 dec. places), it will cause overflow/underflow errors.
The problem with @tomiwa's toFixed
solution is that it rounds the number. I don't want that.
This is how I solved it:
// if USDT supports 6 decimals
// and my input is 153.60000000000002 (more than 6 dec. places)
// it will cause overflow/underflow errors.
// this function trims too long decimals.
function trim_decimal_overflow(n, decimals){
n+=""
if(n.indexOf(".") === -1) return n
const arr = n.split(".");
const fraction = arr[1] .substr(0, decimals);
return arr[0] + "." + fraction;
}
Upvotes: 2
Reputation: 1054
Use toFixed(n)
var stakedAmount = (fixedBalance * 0.50).toFixed(6) ;
n
is defined as the maximum amount of precision you need in your decimal places.
I picked 6 arbitrarily but this comment suggests you could potentially go up to 18?
toFixed(n)
will also convert your number to a string so you can removetoString()
.
I think it's because your floating point number has too many decimal places to be represented by BigNumber
thus, you need to truncate the precision of your decimal places.
For posterity and people coming here via search engines, the full traceback I received was:
Error: fractional component exceeds decimals
(fault="underflow", operation="parseFixed", code=NUMERIC_FAULT, version=bignumber/5.5.0)
It's very interesting because I literally had a similar error just now. A couple of hours after you.
This issue, this issue and this code snippet helped me figure it out.
Upvotes: 10