saifi009 / bitcoinj

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

Patch for additional toNanoCoins() method #1

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Index: Utils.java
===================================================================
--- Utils.java  (revision 17)
+++ Utils.java  (working copy)
@@ -47,6 +47,12 @@
         bi = bi.add(BigInteger.valueOf(cents).multiply(CENT));
         return bi;
     }
+    
+    public static BigInteger toNanoCoins(final CharSequence 
humanFriendlyString)
+    {
+       final float amount = Float.parseFloat(humanFriendlyString.toString());
+       return Utils.toNanoCoins((int) amount, (int) ((amount % 1) * 100));
+    }

     public static void uint32ToByteArrayBE(long val, byte[] out, int offset) {
         out[offset + 0] = (byte) (0xFF & (val >> 24));

Original issue reported on code.google.com by andreas....@gmail.com on 10 Mar 2011 at 10:43

GoogleCodeExporter commented 9 years ago
Thanks for the patch! Just a few comments:

Could you add a JavaDoc explaining what formats are accepted please?

This needs a few unit tests. I have a feeling you need to parse these amounts 
as doubles not floats to represent the full range of possible BitCoin values 
without losing precision. In the unit tests, please check the edge cases like 
the smallest and largest possible values.

Original comment by hearn@google.com on 10 Mar 2011 at 10:51

GoogleCodeExporter commented 9 years ago

Original comment by hearn@google.com on 10 Mar 2011 at 10:51

GoogleCodeExporter commented 9 years ago
Here is an improved version of the patch, which uses BigDecimal instead of 
floating point arithmetics to avoid the precision/rounding problem.

There are also additions to JavaDoc and JUnit (which uncovered issue #11, also 
fixed in this patch).

>  return new BigDecimal(coins).movePointRight(9).toBigIntegerExact();

https://github.com/thiloplanz/bitcoinj/commit/87c45efd57514f203443bace24a253ea0b
bf0483

Maybe we should also look at the spec regarding accepting negative values, see 
the JUnit test case for things that currently pass, but should probably not. Or 
maybe they should, not sure.

Original comment by thilopl...@googlemail.com on 9 Apr 2011 at 2:37

GoogleCodeExporter commented 9 years ago
Okay, since issue #11 was not an issue after all, here is the updated patch 
that does not change the nanocoin terminology or exchange rate, and only does 
what issue #1 originally requested, but with additional Javadoc and JUnit, and 
using BigDecimal instead of floating point arithmetics:

https://github.com/thiloplanz/bitcoinj/compare/8f7a1565...5ee554e

Original comment by thilopl...@googlemail.com on 15 Apr 2011 at 3:04

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r52.

Original comment by hearn@google.com on 20 Apr 2011 at 3:47