saifi009 / bitcoinj

Automatically exported from code.google.com/p/bitcoinj
0 stars 0 forks source link

Utils method to express bitcoin value as a double #25

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I think something like this is needed in order to calculate with Bitcoin 
values, e.g. calculate the value in other currencies:

public static double bitcoinValueToDouble(BigInteger value) {
    BigInteger coins = value.divide(COIN);
    BigInteger cents = value.remainder(COIN);
    return Double.parseDouble(String.format("%d.%08d", coins.intValue(), cents.intValue()));
}

Original issue reported on code.google.com by andreas....@gmail.com on 9 Jun 2011 at 8:56

GoogleCodeExporter commented 9 years ago
I am very much against the use of double, because it is a floating point 
format, which can introduce rounding errors. Floating points should never be 
used in financial transaction systems.

If you want to calculate in BTC (as opposed to microcents/nanocoins/satoshi), 
you should be doing this in BigDecimal. It seems that some work for a converter 
utility class is under way in 
http://code.google.com/p/bitcoinj/issues/detail?id=23 as well.

Original comment by thilopl...@googlemail.com on 9 Jun 2011 at 11:24

GoogleCodeExporter commented 9 years ago
Allright, I am now using the expression

final BigInteger dollars = new BigDecimal(balance).multiply(new 
BigDecimal(exchangeRate)).toBigInteger();

to calculate the dollar value. The only gripe I have is I have to 

useUtils.bitcoinValueToFriendlyString(dollars)

in order to format my Dollars (!) for user reading. This feels awkward to me.

Also, I don't think that floats/doubles can be avoided completely. At some 
point, we might interface into other projects/frameworks that don't support 
BigInteger, like a charting tool.

Original comment by andreas....@gmail.com on 10 Jun 2011 at 1:16

GoogleCodeExporter commented 9 years ago
I think it would be best to keep bitcoinj dealing in exact values using 
BitInteger, and leave further manipulation to the caller.  You can always use 
BigDecimal.doubleValue to convert to a double in the caller.

Original comment by mi...@google.com on 10 Jun 2011 at 10:54

GoogleCodeExporter commented 9 years ago
Integer calculations are the way to go. Introducing float or double for 
currency is wrong on many levels.

Original comment by g.rowe.f...@gmail.com on 12 Jun 2011 at 2:03

GoogleCodeExporter commented 9 years ago
Staying with integers for now.

Original comment by mi...@google.com on 23 Sep 2011 at 8:58