Java BigDecimal getSignedBalance(BigDecimal balance)

Here you can find the source of getSignedBalance(BigDecimal balance)

Description

Utility method to add a sign to a balance
getSignedBalance(100) returns "+100"
getSignedBalance(-100) returns "-100"
getSignedBalance(0) returns "0"

License

Open Source License

Parameter

Parameter Description
balance Balance for which a sign must be added

Return

Signed balance as string

Declaration

public static String getSignedBalance(BigDecimal balance) 

Method Source Code

//package com.java2s;
/*//w  ww . j  a  v  a2 s  .c  o  m
 * Utilities.java
 *
 * Copyright (C) 2009 Francois Duchemin
 *
 * This file is part of GrisbiGraphs.
 *
 * GrisbiGraphs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GrisbiGraphs is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GrisbiGraphs; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    /**
     * Utility method to add a sign to a balance<BR/>
     * getSignedBalance(100) returns "+100"<BR/>
     * getSignedBalance(-100) returns "-100"<BR/>
     * getSignedBalance(0) returns "0"
     * @param balance Balance for which a sign must be added
     * @return Signed balance as string
     */
    public static String getSignedBalance(BigDecimal balance) {
        String signedBalance = "";

        if (balance.compareTo(BigDecimal.ZERO) > 0) {
            signedBalance += "+";
        }
        signedBalance += balance.setScale(2, RoundingMode.HALF_EVEN).toString();

        return signedBalance;
    }
}

Related

  1. getRecordSize(List> columnBaseData)
  2. getRSBigDecimal(Object object)
  3. getScale(BigDecimal bd1, BigDecimal bd2)
  4. getScaledDouble(BigDecimal input)
  5. getSid(BigDecimal total, AtomicLong sid)
  6. getTensVal(BigDecimal val)
  7. getTotalSum(List subTotals)
  8. getUnscaledBytes(BigDecimal bd)
  9. getUpdateAccountsQuery(BigDecimal[] result, int id)