2

I'm working on an electron app HTML based and I'm stuck with a js code that separates numbers with a comma.

On my app, I get the number: 4,344.064 I want to remove and disable the "," as 4344.064

Here, this number is the number of tokens (erc20) held on an ethereum address and when I try to send a transaction with a web3 call (token from my address to another address), I'm only able to send up to 4 token so my app counts only the number before the ",".

I can send 1, 2, 3 or 4 tokens but when I try from 5 I can't so I'm sure that the problem come from the comma.

I've tried a lot of change on my js file but I'm unable to locate this function without removing the ".".

JS CODE :

function updateBalance() {
var address = myWallet.address;
$(".myaddress").html(address);

provider.getBalance(address).then(function(balance) {
    var etherString = ethers.utils.formatEther(balance);
    console.log("ETH Balance: " + etherString);
    var n = parseFloat(etherString);
    var ethValue = n.toLocaleString(
        undefined, // use a string like 'en-US' to override browser 
 locale
        {
            minimumFractionDigits: 2
        }
    );
    var messageEl = $('#ethbal');
    var split = ethValue.split(".");
    ethBalance = parseFloat(ethValue);
    messageEl.html(split[0] + ".<small>" + split[1] + "</small>");
});

var callPromise = tokenContract.functions.balanceOf(address);

callPromise.then(function(result) {
    var trueBal = result[0].toString(10);
    var messageEl = $('#tokenbalance');
    var n = trueBal * 0.00000001;
    console.log("Token Balance: " + n);
    var atyxValue = n.toLocaleString(
        undefined, // use a string like 'en-US' to override browser 
locale
        {
            minimumFractionDigits: 2
        }
    );

    var split = atyxValue.split(".");
    tokenBalance = parseFloat(atyxValue);
    $(".neurealspend").html(atyxValue)
    messageEl.html(split[0] + ".<small>" + split[1] + "</small>");

});

}

Thanks in advance for the help.

0x44N
  • 31
  • 4
  • Could you please provide a working example? So we can help you better. – k3llydev Aug 29 '19 at 15:49
  • Possible duplicate of [javascript parseFloat '500,000' returns 500 when I need 500000](https://stackoverflow.com/questions/3205730/javascript-parsefloat-500-000-returns-500-when-i-need-500000) – Heretic Monkey Aug 29 '19 at 15:55

1 Answers1

1

This is "By Design". The parseFloat function will only consider the parts of the string up until in reaches a non +, -, number, exponent or decimal point. Once it sees the comma it stops looking and only considers the (in "4,344.064" only "4") portion.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/parseFloat To fix this convert the commas to decimal points.

refer - Javascript parse float is ignoring the decimals after my comma

How ever there is a workaround, I would suggest to go like the below,

   //source - https://stackoverflow.com/questions/7571553/javascript-parse-float-is-ignoring-the-decimals-after-my-comma/22453862

console.log(parseFloat(("43,434,344.064").replace(/[^\d\.\-]/g, "")))
Kannan G
  • 974
  • 6
  • 9
  • 1
    This leaves you with '4.344.064' which parseFloat turns into 4344 which loses the numbers after the decimal. It would be better to remove the commas altogether. If it was a larger number like '4,354,343' it would get parsed down to 4354 and completely loses the value. – Alex L Aug 29 '19 at 15:50
  • 1
    updated code to handle such scenario, can you please check @Alex L Thanks for the feedback! – Kannan G Aug 29 '19 at 15:55
  • n = "4,354,343.90"; console.log(parseFloat((n).replace(/[^\d\.\-]/g, ""))) - you need to pass "n" to the parseFloat method instead of "Token Balance:" – Kannan G Aug 29 '19 at 16:18
  • Im now good with tokenBalance = parseFloat(atyxValue.replace(/,/g,'')); thanks very much ! – 0x44N Aug 29 '19 at 16:21
  • you are welcome! thanks for giving opportunity to help you :) @0x44N – Kannan G Aug 29 '19 at 16:22
  • sorry @0x44N i have not worked on electron, but please try on me, will help you to find solution :) – Kannan G Aug 29 '19 at 16:31
  • are you getting any error on the console? please share – Kannan G Aug 29 '19 at 16:36
  • let me check give me few min – Kannan G Aug 29 '19 at 16:54
  • can you please read this page, you need to run --asar with config again, read this https://github.com/electron/electron-packager/issues/35 – Kannan G Aug 29 '19 at 17:03