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, 2022 in Blockchain by Rahul
• 9,680 points
617 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, 2022 by Aditya
• 7,680 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
963 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,720 points
1,345 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,220 points
677 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,370 points
843 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,140 points
1,389 views
0 votes
0 answers

what does bash: /home/XXX/.rvm/scripts/rvm: No such file or directory mean?

I was installing the ruby on rails ...READ MORE

May 26, 2022 in Others by Ashwini
• 5,430 points
948 views
0 votes
1 answer

Convert Excel and Word files to PDF Using ruby

 You can combine some: For excel files - ...READ MORE

answered Sep 26, 2022 in Others by narikkadan
• 63,600 points
1,321 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, 2022 in Blockchain by Aditya
• 7,680 points
1,103 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, 2022 in Blockchain by Aditya
• 7,680 points
945 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