0

I get the value of money in Satoshi and I would like to have it in BTC. I develop the app in Java/Maven/JSP and use BitcoinJ for the framework. I use the code,

    public Coin getBalance() {

        int fac = (int) Math.pow(10, 8);
        return balance.div(fac);
    }

In the JSP, I use,

<%
    DecimalFormat decimalFormat = new DecimalFormat("#0.00");
%>
<div class="fild_value">
                    <%= decimalFormat.format(model.getBalance().getValue()) %>&nbsp; BTC
</div>

In this way, I miss the value after the decimal. For example, I get 1.00 BTC when I suppose to get 1.68 BTC. How to convert Satoshi to BTC in Java properly?

  • 1
    value times 100million ? – RaisingAgent May 31 '17 at 9:44
  • The balance provides the value in satoshi and the mentioned code in the getBalance method converts it to the BTC. [1 BTC = 10^8 Satoshi]. Now, after converting in the fashion, I was not getting the value after the decimal. How to get the value properly? – Arefe May 31 '17 at 9:50
  • 1
    Note: int/int = int, so 4/3 =1. I am not aware of the specifics of your problem, but the solution must to convert either the balance in satoshi or fac(10^8) to float and use it for division. – sanket1729 May 31 '17 at 11:50
  • Tried the way suggested and doesn't work for me. – Arefe Jun 1 '17 at 4:17
1

The Coin.valueOf(long) static constructor takes Satoshis as an input. You can use one of the toString/toFriendlyString/toPlainString methods to derive a human readable value string.

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.