2

I work with Java/Maven/JSP app with BitcoinJ and I have transfer about 0.0048 BTC from a testnet faucet. However, after receiving, I see that my account has balance of 480000.00 BTC.

enter image description here

This is the method I have used to get the Satoshi Coin.

public Coin getBalance() {
        return balance;
    }

I have checked the code and at least my backend part looks normal to me. How to approach to this problem?

3

Apparently the app shows the amount as Satoshi (the smallest unit) instead of BTC. For internal calculations that's ok because it avoids rounding errors, but for input and output you should scale by an appropriate factor (10^8 for BTC, or 10^5 for mBTC).

2

Yes, and in bitcoinj there are actually two classes for formatting Coin values for humans: MonetaryFormat and BtcFormat. I strongly suggest using one of the two.

Here's an example:

Wallet wallet = <initialize wallet here>
String friendlyFormat = BtcFormat.getInstance().format(wallet.getBalance());
  • How to convert Satoshies to BTC in Java? – U2m May 31 '17 at 7:51
  • What does the wallet.getBalance() returns? I have tried with several ways and none actually works for me. The method I use to get the Satoshi Coin is public Coin getBalance() { return balance; } How can I get the correct value in BTC? I have also tried with balance.leftShift(8) – U2m Jun 1 '17 at 4:21

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.