简体   繁体   中英

Floating point number calculation

I'm new to working with bitcoin and I made a javascript that adds 10 satoshi every second and then it displays on the screen in BTC.

Can someone show me why it starts showing strange numbers and then the decimal points are incorrect? I need it to start with 0.00000010 BTC

 var start = 0; window.setInterval( function() { start = start + 10; var btc = start / 100000000; console.log(btc + " BTC"); }, 1000); 

By using toFixed() you get the results:-)

 var start = 0 ; window.setInterval(function () { start = start + 10; var btc = parseFloat(start / 100000000).toFixed(8); console.log(btc + " BTC"); }, 1000); 

toFixed() method will do the trick in this case, and you will also have to increment with 0.00000010 if you want it to start from that. See the working snippet below please:

 var start = 0; window.setInterval( function() { start = start + 0.00000010; var btc = start.toFixed(8); document.getElementById("start").innerHTML = btc + " BTC"; }, 1000); 
 <p id='start'></p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM