How to round a number down to 8 decimal places if its over 8 decimal places in Javascript

0 votes

I'm trying to check if a number inputed has over 8 decimal places and if it does then I want to round it back down to 8 decimal places. However, when I input the number 1.234001, it automatically rounds it to 8 decimal places. (1.234001 / 0.00000001) % 1 = 0 so I'm not sure why its rounding it. Here's my code

var SAT = 0.00000001;
if(!isNaN(input.value) && ((input.value / SAT) % 1 != 0)) {
                input.value = parseFloat(input.value).toFixed(8);
                console.log(6);
            }
Sep 12, 2018 in Blockchain by digger
• 27,640 points
36 views

1 answer to this question.

0 votes

Try it in this way:

function nrOfDecimals(number) {
    var match = (''+number).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/);
    if (!match) { return 0; }

    var decimals =  Math.max(0,
       (match[1] ? match[1].length : 0)
       // Correct the notation.
       - (match[2] ? +match[2] : 0));

     if(decimals > 8){
        //if decimal are more then 8
        number = parseFloat(number).toFixed(8);
     }
     //else no adjustment is needed
     return number;
}
answered Sep 12, 2018 by slayer
• 29,050 points

Related Questions In Blockchain

0 votes
1 answer

How to run a Javascript in truffle console?

There are two ways to do this. You ...READ MORE

answered Jan 24 in Blockchain by Omkar
• 67,380 points
70 views
0 votes
1 answer

How to make sure transactions take no fee in a private Ethereum blockchain?

In a private ethereum network you have ...READ MORE

answered Mar 26, 2018 in Blockchain by Christine
• 15,790 points

edited Mar 26, 2018 by Christine 93 views
+1 vote
2 answers

Can I include real world data in a smart contract? If so, how?

You cant access/embed real world data using ...READ MORE

answered Jul 17, 2018 in Blockchain by sapan
57 views
+1 vote
3 answers

How efficient is Ethereum in regard to its TPS??

TPS means transactions processing at any given ...READ MORE

answered Apr 16, 2018 in Blockchain by Shashank
• 10,350 points
1,298 views
0 votes
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by ariaholic
• 7,340 points
75 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
138 views
0 votes
1 answer

Javascript: cant update assets in for-loop

Your for-loop will not wait for the ...READ MORE

answered Sep 17, 2018 in Blockchain by slayer
• 29,050 points
26 views
0 votes
1 answer

How to Check if string is in list in javascript?

At first, make sure that the string ...READ MORE

answered Sep 19, 2018 in Blockchain by slayer
• 29,050 points
47 views