javajava-mebigdecimalarbitrary-precisioncldc

Alternatives for arbitrary-precision decimals in J2ME


We're making some modifications to a legacy project using J2ME. Until now, decimal numbers were treated as Strings since no arithmetic operations were needed. The values were only displayed as text.

Now, arbitrary-precision arithmetic is required. If it were Java SE, I'd use BigDecimal, but it is not present in the MIDP/CLDC API.

I was trying to work out my custom DecimalNumber class, but while I was fixing some bugs and discovering new ones in the unit tests, I realized that it will get some time to make this class rock-solid and error-free.

So, instead of reinventing the wheel, what alternatives could I reuse for this purpose? For instance, can the BigInteger and BigDecimal classes be ported to J2ME (CLDC1.1)? I've read on other question someone trying to port JavaSE's BigDecimal with bouncycastle's BigInteger. Are these compatible?

Any help will be appreciated.


Solution

  • I successfully ported JavaSE's BigDecimal. I had to port also these other classes:

    Basically I had to remove generics, some serialization methods, almost every method in BigInteger related to primes, and replace int[].clone() with a similar method. Also tweaking the compareTo methods.

    My goal was just to achieve arbitrary-precision and convert from String to BigDecimal, I didn't really need anything else.

    UPDATE: Not working!!! Seems that when retrieving the source code, I mixed classes from different sources (ones were from OpenJDK, Oracle JavaSE, ...). These were all for Java 6, but I've noticed some major changes between different version releases. It turns out that they are not interoperating well (or either some of them contain serious bugs, but I don't think so) so the port has been a BIG fail. I need a to solve this ASAP, so now I'm looking for the following alternatives:

    UPDATE:
    I finally used PayPal's BigDecimal port contained in their Mobility Payment Library for BlackBerry. BlackBerry is based on J2ME, so it is perfect for the task. I've made a considerable amount of unit tests on it and I can say that it is consistent with the behavior of JavaSE's BigDecimal.