Java BigInteger Calculate bitcoinValueToFriendlyString(BigInteger value)

Here you can find the source of bitcoinValueToFriendlyString(BigInteger value)

Description

Returns the given value in nanocoins as a 0.12 type string.

License

Apache License

Declaration

public static String bitcoinValueToFriendlyString(BigInteger value) 

Method Source Code

//package com.java2s;
/**// ww w  .  jav  a 2  s . c  o  m
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.math.BigDecimal;
import java.math.BigInteger;

public class Main {
    /**
     * Returns the given value in nanocoins as a 0.12 type string. More digits after the decimal place will be used
     * if necessary, but two will always be present.
     */
    public static String bitcoinValueToFriendlyString(BigInteger value) {
        // TODO: This API is crap. This method should go away when we encapsulate money values.
        boolean negative = value.signum() < 0;
        if (negative)
            value = value.negate();
        BigDecimal bd = new BigDecimal(value, 8);
        String formatted = bd.toPlainString(); // Don't use scientific notation.
        int decimalPoint = formatted.indexOf(".");
        // Drop unnecessary zeros from the end.
        int toDelete = 0;
        for (int i = formatted.length() - 1; i > decimalPoint + 2; i--) {
            if (formatted.charAt(i) == '0')
                toDelete++;
            else
                break;
        }
        return (negative ? "-" : "") + formatted.substring(0, formatted.length() - toDelete);
    }
}

Related

  1. areEqual(BigInteger[] a, BigInteger[] b)
  2. base128(BigInteger val)
  3. base62Encode(BigInteger number)
  4. bitArrayToUnsignedBigInteger(int[] bits)
  5. byte_size(BigInteger bi)
  6. calculateGx(BigInteger p, BigInteger g, BigInteger x)
  7. calculateRx_(BigInteger Rx, BigInteger n)
  8. castToBigInteger(Object val)