How to store cryptocurrency balance without causing floating point errors in ruby rails

0 votes
I'm creating a rails app where users will have an account balance in Ethereum and possibly other cryptocurrencies. Currently, I am storing the user's balance as afloat. This, however, is a really bad approach as cryptocurrencies are usually divisible down to 8 digits so any floating-point error will cause a huge loss/gain in funds. How can I store the user's balance (what datatype should I use) so as to not cause floating-point errors? Thanks
Apr 5 in Blockchain by Rahul
• 8,980 points
24 views

1 answer to this question.

0 votes

 I suggest storing the values as integers. Just multiply the currency values by a million or so when you store them and convert them as needed in your application.Ruby has a special class for this:

BigDecimal -> https://ruby-doc.org/stdlib-2.4.0/libdoc/bigdecimal/rdoc/BigDecimal.html

answered Apr 7 by Aditya
• 7,280 points

Related Questions In Blockchain

+1 vote
1 answer

How to store state data in Ethereum blockchain?

You won't have to overwrite the whole ...READ MORE

answered Apr 25, 2018 in Blockchain by Shashank
• 10,400 points
367 views
0 votes
1 answer

How to store picture(s) in a hyperledger blockchain channel

You can hold images as encrypted characters ...READ MORE

answered Jun 16, 2018 in Blockchain by charlie_brown
• 7,710 points
801 views
0 votes
1 answer

How to make transactions without creating assets in multichain?

To transfer your Mycoins, you can make ...READ MORE

answered Jul 3, 2018 in Blockchain by Omkar
• 69,190 points
283 views
0 votes
1 answer

How to store photos in Hyperledger smart contract?

To do this, you need to represent ...READ MORE

answered Jul 9, 2018 in Blockchain by slayer
• 29,310 points
305 views
0 votes
1 answer
0 votes
1 answer

How do I install Ruby on Rails?

Hi @Anvi, what @Pratibha has mentioned is ...READ MORE

answered Mar 1, 2019 in Others by Abha
• 28,100 points
265 views
0 votes
1 answer

Is it possible to access Couchbase by terminal?

What you want to accomplish isn't possible ...READ MORE

answered Apr 29, 2018 in DevOps & Agile by DareDev
• 6,890 points
231 views
0 votes
1 answer

How to connect Java program to the MySQL database?

You can connect your Java code with ...READ MORE

answered May 11, 2018 in Java by Parth
• 4,630 points
851 views
0 votes
1 answer

On-Chain NFT: How to store a pixel image on the ethereum blockchain?

The images are usually not stored in ...READ MORE

answered Feb 24 in Blockchain by Aditya
• 7,280 points
201 views
0 votes
1 answer

Is it possible to get wallet balance passing the wallet address on any cryptocurrency?

For Ethereum the API is called web3.eth.getBalance. There ...READ MORE

answered Apr 7 in Blockchain by Aditya
• 7,280 points
18 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP