Java BigDecimal Format formatQuantity(BigDecimal quantity)

Here you can find the source of formatQuantity(BigDecimal quantity)

Description

format Quantity

License

Mozilla Public License

Declaration

public static String formatQuantity(BigDecimal quantity) 

Method Source Code

//package com.java2s;
/**//from w  ww .  j  a  va2 s  .co m
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Contributor(s): Contributors are attributed in the source code
* where applicable.
*
* The Original Code is "Dosis-til-tekst".
*
* The Initial Developer of the Original Code is Trifork Public A/S.
*
* Portions created for the FMK Project are Copyright 2011,
* National Board of e-Health (NSI). All Rights Reserved.
*/

import java.math.BigDecimal;

public class Main {
    public static String formatQuantity(BigDecimal quantity) {
        // We replace . with , below using string replace as we want to make
        // sure we always use , no matter what the locale settings are
        return trim(quantity.toPlainString().replace('.', ','));
    }

    public static String trim(String number) {
        if (number.indexOf('.') < 0 && number.indexOf(',') < 0)
            return number;
        if (number.length() == 1 || number.charAt(number.length() - 1) > '0')
            return number;
        else
            return trim(number.substring(0, number.length() - 1));
    }
}

Related

  1. formatoDecimal(String tipo, BigDecimal valor)
  2. formatPercent(BigDecimal percent)
  3. formatPercentage(BigDecimal percentage)
  4. formatPercentDecimalDouble(Object input)
  5. formatQuantity(BigDecimal quantity)
  6. formatRate(BigDecimal rate)
  7. formatRateForDisplay(BigDecimal rate)
  8. formatRight(BigDecimal pNombre, NumberFormat pNf, int pMaxNumberOfDigit)
  9. formatServiceSpecificDate(Date date)