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
• 26,740 points
1,528 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,370 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, 2019 in Blockchain by Omkar
• 69,220 points
2,536 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 1,594 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 18, 2018 in Blockchain by sapan
1,214 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,400 points
3,830 views
+1 vote
1 answer

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

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,460 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
1,947 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,370 points
645 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,470 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,370 points
3,162 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP