Example usage for java.text DecimalFormat setDecimalSeparatorAlwaysShown

List of usage examples for java.text DecimalFormat setDecimalSeparatorAlwaysShown

Introduction

In this page you can find the example usage for java.text DecimalFormat setDecimalSeparatorAlwaysShown.

Prototype

public void setDecimalSeparatorAlwaysShown(boolean newValue) 

Source Link

Document

Allows you to set the behavior of the decimal separator with integers.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DecimalFormat format = new DecimalFormat();
    format.setDecimalSeparatorAlwaysShown(false);
    System.out.println(format.format(123456789.12345678));

}

From source file:Main.java

public static final String objectToString(Object obj, DecimalFormat fmt) {
    fmt.setDecimalSeparatorAlwaysShown(false);
    if (obj instanceof Double)
        return fmt.format(((Double) obj).doubleValue());
    if (obj instanceof Long)
        return fmt.format(((Long) obj).longValue());
    else/*from   w  ww .j av a2 s  .c om*/
        return obj.toString();
}

From source file:Main.java

public static String formatCurrency(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);/*from w ww .  ja va  2  s. com*/
    return df.format(amount);
}

From source file:Main.java

public static String formatNumber(double amount, int precision, String pattern, Locale locale) {
    NumberFormat nf = NumberFormat.getNumberInstance(locale);
    DecimalFormat df = (DecimalFormat) nf;
    df.setMinimumFractionDigits(precision);
    df.setMaximumFractionDigits(precision);
    df.setDecimalSeparatorAlwaysShown(true);
    df.applyPattern(pattern);//from   w w  w .j  av a 2  s  .  c  o m
    return df.format(amount);
}

From source file:StringUtils.java

/**
 * Format a percentage for presentation to the user.
 * @param done the percentage to format (0.0 to 1.0)
 * @param digits the number of digits past the decimal point
 * @return a string representation of the percentage
 *///from   www .j  av  a  2  s  .c  om
public static String formatPercent(double done, int digits) {
    DecimalFormat percentFormat = new DecimalFormat("0.00%");
    double scale = Math.pow(10.0, digits + 2);
    double rounded = Math.floor(done * scale);
    percentFormat.setDecimalSeparatorAlwaysShown(false);
    percentFormat.setMinimumFractionDigits(digits);
    percentFormat.setMaximumFractionDigits(digits);
    return percentFormat.format(rounded / scale);
}

From source file:pl.otros.vfs.browser.table.FileSize.java

private static String format(final long value, final long divider, final String unit) {
    final double result = divider > 1 ? (double) value / (double) divider : (double) value;
    DecimalFormat decimalFormat = new DecimalFormat();
    decimalFormat.setMaximumFractionDigits(1);
    decimalFormat.setMinimumFractionDigits(0);
    decimalFormat.setGroupingUsed(false);
    decimalFormat.setDecimalSeparatorAlwaysShown(false);
    return decimalFormat.format(result) + " " + unit;
}

From source file:com.prowidesoftware.swift.utils.SwiftFormatUtils.java

/**
 * Parses a Number into a SWIFT string number ####,## with truncated zero decimals and mandatory decimal separator.
 * <ul>/*w  w w  . j a  v  a 2 s.c  o m*/
 *    <li>Example: 1234.00 -> 1234,</li>
 *    <li>Example: 1234 -> 1234,</li>
 *    <li>Example: 1234.56 -> 1234,56</li>
 * </ul>
 * @param number to parse
 * @return Number of the parsed amount or <code>null</code> if the number is null
 */
public static String getNumber(final Number number) {
    if (number != null) {
        final DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setDecimalSeparator(',');
        final DecimalFormat df = new DecimalFormat("0.##########", symbols);
        df.setParseBigDecimal(true);
        df.setDecimalSeparatorAlwaysShown(true);
        final String formatted = df.format(number);
        final String result = StringUtils.replaceChars(formatted, '.', ',');
        return result;
    }
    return null;
}

From source file:org.biokoframework.system.repository.sql.translator.annotation.impl.DoubleTranslator.java

@Override
public String convertFromDBValue(String fieldName, ResultSet resultset, Field fieldAnnotation)
        throws SQLException {

    if (StringUtils.isEmpty(fieldAnnotation.format())) {
        DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH);
        decimalFormat.setDecimalSeparatorAlwaysShown(false);
        decimalFormat.setMaximumFractionDigits(50);

        return decimalFormat.format(resultset.getDouble(fieldName));
    }/*  ww  w .  ja va  2 s . com*/

    DecimalFormat decimalFormat = new DecimalFormat(fieldAnnotation.format());
    //      DecimalFormat decimalFormat = new DecimalFormat();
    //      decimalFormat.setDecimalSeparatorAlwaysShown(false);

    return decimalFormat.format(resultset.getDouble(fieldName));
}

From source file:com.ancientprogramming.fixedformat4j.format.impl.AbstractDecimalFormatter.java

public String asString(T obj, FormatInstructions instructions) {
    BigDecimal roundedValue = null;
    int decimals = instructions.getFixedFormatDecimalData().getDecimals();
    if (obj != null) {
        BigDecimal value = obj instanceof BigDecimal ? (BigDecimal) obj : BigDecimal.valueOf(obj.doubleValue());

        RoundingMode roundingMode = instructions.getFixedFormatDecimalData().getRoundingMode();

        roundedValue = value.setScale(decimals, roundingMode);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Value before rounding = '" + value + "', value after rounding = '" + roundedValue
                    + "', decimals = " + decimals + ", rounding mode = " + roundingMode);
        }/*from   w w  w  .j  a  v  a  2 s .  c  om*/
    }

    DecimalFormat formatter = new DecimalFormat();
    formatter.setDecimalSeparatorAlwaysShown(true);
    formatter.setMaximumFractionDigits(decimals);

    char decimalSeparator = formatter.getDecimalFormatSymbols().getDecimalSeparator();
    char groupingSeparator = formatter.getDecimalFormatSymbols().getGroupingSeparator();
    String zeroString = "0" + decimalSeparator + "0";

    String rawString = roundedValue != null ? formatter.format(roundedValue) : zeroString;
    if (LOG.isDebugEnabled()) {
        LOG.debug("rawString: " + rawString + " - G[" + groupingSeparator + "] D[" + decimalSeparator + "]");
    }
    rawString = rawString.replaceAll("\\" + groupingSeparator, "");
    boolean useDecimalDelimiter = instructions.getFixedFormatDecimalData().isUseDecimalDelimiter();

    String beforeDelimiter = rawString.substring(0, rawString.indexOf(decimalSeparator));
    String afterDelimiter = rawString.substring(rawString.indexOf(decimalSeparator) + 1, rawString.length());
    if (LOG.isDebugEnabled()) {
        LOG.debug("beforeDelimiter[" + beforeDelimiter + "], afterDelimiter[" + afterDelimiter + "]");
    }

    //trim decimals
    afterDelimiter = StringUtils.substring(afterDelimiter, 0, decimals);
    afterDelimiter = StringUtils.rightPad(afterDelimiter, decimals, '0');

    String delimiter = useDecimalDelimiter ? "" + instructions.getFixedFormatDecimalData().getDecimalDelimiter()
            : "";
    String result = beforeDelimiter + delimiter + afterDelimiter;
    if (LOG.isDebugEnabled()) {
        LOG.debug("result[" + result + "]");
    }
    return result;
}

From source file:com.blackbear.flatworm.converters.CoreConverters.java

public String convertBigDecimal(Object obj, Map<String, ConversionOption> options) {
    if (obj == null) {
        return null;
    }//from  w w w . j  a  v  a  2 s  .  co  m
    BigDecimal bd = (BigDecimal) obj;

    int decimalPlaces = 0;
    String decimalPlacesOption = (String) Util.getValue(options, "decimal-places");
    boolean decimalImplied = "true".equals(Util.getValue(options, "decimal-implied"));

    if (decimalPlacesOption != null)
        decimalPlaces = Integer.parseInt(decimalPlacesOption);

    DecimalFormat format = new DecimalFormat();
    format.setDecimalSeparatorAlwaysShown(!decimalImplied);
    format.setMinimumFractionDigits(decimalPlaces);
    format.setMaximumFractionDigits(decimalPlaces);

    return format.format(bd.doubleValue());
}