Java Code Examples for java.text.DecimalFormat#setDecimalSeparatorAlwaysShown()

The following examples show how to use java.text.DecimalFormat#setDecimalSeparatorAlwaysShown() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Polynomial.java    From JavaMainRepo with Apache License 2.0 6 votes vote down vote up
public String toString() {
	DecimalFormat format = new DecimalFormat();
	format.setDecimalSeparatorAlwaysShown(false);
	String pol = "";
	int d = getDegree();
	for (int i = d; i >= 0; i--) {
		if (i == 1) {
			if (coeffs[0] >= 0) {
				pol += format.format(coeffs[i]) + "X+";
			} else {
				pol += format.format(coeffs[i]) + "X";
			}
		} else if (i == 0) {
			pol += format.format(coeffs[i]);
		} else {
			if (coeffs[i - 1] >= 0) {
				pol += format.format(coeffs[i]) + "X^" + i + "+";
			} else {
				pol += format.format(coeffs[i]) + "X^" + i;
			}
		}
	}
	return pol;
}
 
Example 2
Source File: NumberFormatProviderImpl.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private NumberFormat getInstance(Locale locale,
                                        int choice) {
    if (locale == null) {
        throw new NullPointerException();
    }

    LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
    String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
    DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);

    if (choice == INTEGERSTYLE) {
        format.setMaximumFractionDigits(0);
        format.setDecimalSeparatorAlwaysShown(false);
        format.setParseIntegerOnly(true);
    } else if (choice == CURRENCYSTYLE) {
        adjustForCurrencyDefaultFractionDigits(format, symbols);
    }

    return format;
}
 
Example 3
Source File: AbstractGraphicsDocument.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Initialise the abstract graphics document, specifically meant to set the formatter.
 *
 * @param defaultMaxDigits default max digits
 */
public AbstractGraphicsDocument(int defaultMaxDigits) {
	Locale locale = new Locale("en", "US");
	DecimalFormatSymbols decimalSymbols = new DecimalFormatSymbols(locale);
	decimalSymbols.setDecimalSeparator('.');
	formatter = new DecimalFormat();
	formatter.setDecimalFormatSymbols(decimalSymbols);

	// do not group
	formatter.setGroupingSize(0);

	// do not show decimal SEPARATOR if it is not needed
	formatter.setDecimalSeparatorAlwaysShown(false);
	formatter.setGroupingUsed(false);

	// set default number of fraction digits
	formatter.setMaximumFractionDigits(defaultMaxDigits);

	// minimum fraction digits to 0 so they get not rendered if not needed
	formatter.setMinimumFractionDigits(0);
}
 
Example 4
Source File: NumberFormatProviderImpl.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private NumberFormat getInstance(Locale locale,
                                        int choice) {
    if (locale == null) {
        throw new NullPointerException();
    }

    LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
    String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
    DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);

    if (choice == INTEGERSTYLE) {
        format.setMaximumFractionDigits(0);
        format.setDecimalSeparatorAlwaysShown(false);
        format.setParseIntegerOnly(true);
    } else if (choice == CURRENCYSTYLE) {
        adjustForCurrencyDefaultFractionDigits(format, symbols);
    }

    return format;
}
 
Example 5
Source File: NumberFormatProviderImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private NumberFormat getInstance(Locale locale,
                                        int choice) {
    if (locale == null) {
        throw new NullPointerException();
    }

    LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
    String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
    DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);

    if (choice == INTEGERSTYLE) {
        format.setMaximumFractionDigits(0);
        format.setDecimalSeparatorAlwaysShown(false);
        format.setParseIntegerOnly(true);
    } else if (choice == CURRENCYSTYLE) {
        adjustForCurrencyDefaultFractionDigits(format, symbols);
    }

    return format;
}
 
Example 6
Source File: NumberFormatProviderImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private NumberFormat getInstance(Locale locale,
                                        int choice) {
    if (locale == null) {
        throw new NullPointerException();
    }

    LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
    String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
    DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);

    if (choice == INTEGERSTYLE) {
        format.setMaximumFractionDigits(0);
        format.setDecimalSeparatorAlwaysShown(false);
        format.setParseIntegerOnly(true);
    } else if (choice == CURRENCYSTYLE) {
        adjustForCurrencyDefaultFractionDigits(format, symbols);
    }

    return format;
}
 
Example 7
Source File: NumberFormatProviderImpl.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private NumberFormat getInstance(Locale locale,
                                        int choice) {
    if (locale == null) {
        throw new NullPointerException();
    }

    LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
    String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
    DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);

    if (choice == INTEGERSTYLE) {
        format.setMaximumFractionDigits(0);
        format.setDecimalSeparatorAlwaysShown(false);
        format.setParseIntegerOnly(true);
    } else if (choice == CURRENCYSTYLE) {
        adjustForCurrencyDefaultFractionDigits(format, symbols);
    }

    return format;
}
 
Example 8
Source File: NumberFormatProviderImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private NumberFormat getInstance(Locale locale,
                                        int choice) {
    if (locale == null) {
        throw new NullPointerException();
    }

    LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
    String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
    DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);

    if (choice == INTEGERSTYLE) {
        format.setMaximumFractionDigits(0);
        format.setDecimalSeparatorAlwaysShown(false);
        format.setParseIntegerOnly(true);
    } else if (choice == CURRENCYSTYLE) {
        adjustForCurrencyDefaultFractionDigits(format, symbols);
    }

    return format;
}
 
Example 9
Source File: NumberFormatProviderImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private NumberFormat getInstance(Locale locale,
                                        int choice) {
    if (locale == null) {
        throw new NullPointerException();
    }

    LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
    String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
    DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);

    if (choice == INTEGERSTYLE) {
        format.setMaximumFractionDigits(0);
        format.setDecimalSeparatorAlwaysShown(false);
        format.setParseIntegerOnly(true);
    } else if (choice == CURRENCYSTYLE) {
        adjustForCurrencyDefaultFractionDigits(format, symbols);
    }

    return format;
}
 
Example 10
Source File: NumberFormatProviderImpl.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private NumberFormat getInstance(Locale locale,
                                        int choice) {
    if (locale == null) {
        throw new NullPointerException();
    }

    LocaleProviderAdapter adapter = LocaleProviderAdapter.forType(type);
    String[] numberPatterns = adapter.getLocaleResources(locale).getNumberPatterns();
    DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
    int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
    DecimalFormat format = new DecimalFormat(numberPatterns[entry], symbols);

    if (choice == INTEGERSTYLE) {
        format.setMaximumFractionDigits(0);
        format.setDecimalSeparatorAlwaysShown(false);
        format.setParseIntegerOnly(true);
    } else if (choice == CURRENCYSTYLE) {
        adjustForCurrencyDefaultFractionDigits(format, symbols);
    }

    return format;
}
 
Example 11
Source File: RangeEditorDialog.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
DoubleFormatter(String pattern) {
    final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
    format = new DecimalFormat(pattern, decimalFormatSymbols);

    format.setParseIntegerOnly(false);
    format.setParseBigDecimal(false);
    format.setDecimalSeparatorAlwaysShown(true);
}
 
Example 12
Source File: DecimalFormatter.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
public DecimalFormatter(String pattern) {
    final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
    format = new DecimalFormat(pattern, decimalFormatSymbols);
    format.setParseIntegerOnly(false);
    format.setParseBigDecimal(false);
    format.setDecimalSeparatorAlwaysShown(true);
}
 
Example 13
Source File: ConvertUtils.java    From aes-rsa-java with Apache License 2.0 5 votes vote down vote up
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
        return obj.toString();
}
 
Example 14
Source File: Utils.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public static String formatPercent(double done, int digits) {
	DecimalFormat percentFormat = new DecimalFormat("0.0%");
	percentFormat.setDecimalSeparatorAlwaysShown(false);
	percentFormat.setMinimumFractionDigits(digits);
	percentFormat.setMaximumFractionDigits(digits);
	return percentFormat.format(done);

}
 
Example 15
Source File: DoubleTools.java    From JANNLab with GNU General Public License v3.0 5 votes vote down vote up
public static String asString(final double value, final int decimals) {
    //
    DecimalFormat f = new DecimalFormat();
    f.setDecimalSeparatorAlwaysShown(true);
    f.setMaximumFractionDigits(decimals);
    f.setMinimumFractionDigits(decimals);
    f.setGroupingUsed(false);
    //
    f.setDecimalFormatSymbols(new DecimalFormatSymbols() {
        private static final long serialVersionUID = -2464236658633690492L;
        public char getGroupingSeparator() { return ' '; }
        public char getDecimalSeparator() { return '.'; }
    });
    return f.format(value);    
}
 
Example 16
Source File: StringUtils.java    From Canova with Apache License 2.0 5 votes vote down vote up
/**
 * 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
 */
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);
}
 
Example 17
Source File: CurrencyCache.java    From financisto with GNU General Public License v2.0 5 votes vote down vote up
public static DecimalFormat createCurrencyFormat(Currency c) {
	DecimalFormatSymbols dfs = new DecimalFormatSymbols();
	dfs.setDecimalSeparator(charOrEmpty(c.decimalSeparator, dfs.getDecimalSeparator()));
	dfs.setGroupingSeparator(charOrEmpty(c.groupSeparator, dfs.getGroupingSeparator()));
	dfs.setMonetaryDecimalSeparator(dfs.getDecimalSeparator());
	dfs.setCurrencySymbol(c.symbol);

	DecimalFormat df = new DecimalFormat("#,##0.00", dfs);
	df.setGroupingUsed(dfs.getGroupingSeparator() > 0);
	df.setMinimumFractionDigits(c.decimals);
	df.setMaximumFractionDigits(c.decimals);
	df.setDecimalSeparatorAlwaysShown(false);
	return df;
}
 
Example 18
Source File: NumberTextWatcher.java    From fingen with Apache License 2.0 5 votes vote down vote up
public NumberTextWatcher(EditText et, TextWatcher textWatcher) {
    mTextWatcher = textWatcher;
    DecimalFormatSymbols symbols = new DecimalFormatSymbols();
    symbols.setDecimalSeparator('.');
    symbols.setGroupingSeparator(' ');
    df = new DecimalFormat("#,###.##");
    df.setDecimalSeparatorAlwaysShown(true);
    df.setDecimalFormatSymbols(symbols);
    dfnd = new DecimalFormat("#,###");
    dfnd.setDecimalFormatSymbols(symbols);
    this.et = et;
    hasFractionalPart = false;
}
 
Example 19
Source File: Komi.java    From FancyBing with GNU General Public License v3.0 5 votes vote down vote up
public String toString()
{
    DecimalFormat format =
        (DecimalFormat)(NumberFormat.getInstance(Locale.ENGLISH));
    format.setGroupingUsed(false);
    format.setDecimalSeparatorAlwaysShown(false);
    return format.format(m_value);
}
 
Example 20
Source File: DateTimeFormatter.java    From incubator-hivemall with Apache License 2.0 4 votes vote down vote up
private static String formatNumber(final double number, boolean commaSep) {
    DecimalFormat f = new DecimalFormat(commaSep ? "#,###.###" : "###.###");
    f.setDecimalSeparatorAlwaysShown(false);
    return f.format(number);
}