I am developing a bitcoin wallet for education purposes, and i'm working on a getbalance method that given an address i return the balance of the address.

Problem is, when communicating via the RPC protocol i am given the amount in a double format rather than satoshi (int). And i would like to provide the amount in Satoshi's.

How do i properly convert the double amount, into uint64 satoshi amount? Since double is not an accurate value, how do I make sure that my "users" get the correct value for their balance, or on their transactions?



The amounts returned by Bitcoin Core are accurate. They are not actually doubles; they're just output that way for convenience. Bitcoin Core internally uses a int64 and the amounts are satoshis. When outputting for users, the int64 is used and a string formatter just puts a decimal point in the appropriate place. Since it already is exact, you just need to drop the decimal point. The easiest way to do this is to take the output as a string, remove the decimal point, and convert that string to an integer.

  • Thank you for your quick reply! I'm using jsonrpccpp to communicate with the bitcoin core using RPC. Since you say that the bitcoin core is accurate, the jsonrpccpp allows me to parse the information and view the amount as Double, String etc. Double seems to be the correct amount. how do i make sure, that the parser it self stores it correctly?
    – Haim
    Mar 13 '19 at 9:28
  • I suggest that you store it as a string and not as a double.
    – Andrew Chow
    Mar 13 '19 at 14:40
  • i tried your approach. I've converted 0.00018281 BTC to Satoshi's in two ways. from double i got 18281 Satoshi's and from string i got 18280999999999999 satoshi's. it seems like the string isn't converting the value properly (from the jsonrpccpp). any suggestions? (all i did in the string was to remove the '.' character)
    – Haim
    Mar 14 '19 at 7:27
  • Sorry, I am not familiar with how jsonrpccpp works.
    – Andrew Chow
    Mar 14 '19 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.