Java Code Examples for java.text.DecimalFormat.getDecimalFormatSymbols()

The following are Jave code examples for showing how to use getDecimalFormatSymbols() of the java.text.DecimalFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: micrometer   File: DoubleFormat.java   Source Code and License Vote up 6 votes
@Override
protected NumberFormat initialValue() {

    // Always create the formatter for the US locale in order to avoid this bug:
    // https://github.com/indeedeng/java-dogstatsd-client/issues/3
    final NumberFormat numberFormatter = NumberFormat.getInstance(Locale.US);
    numberFormatter.setGroupingUsed(false);
    numberFormatter.setMaximumFractionDigits(6);

    // we need to specify a value for Double.NaN that is recognized by dogStatsD
    if (numberFormatter instanceof DecimalFormat) { // better safe than a runtime error
        final DecimalFormat decimalFormat = (DecimalFormat) numberFormatter;
        final DecimalFormatSymbols symbols = decimalFormat.getDecimalFormatSymbols();
        symbols.setNaN("NaN");
        decimalFormat.setDecimalFormatSymbols(symbols);
    }

    return numberFormatter;
}
 
Example 2
Project: myfaces-trinidad   File: NumberConverter.java   Source Code and License Vote up 6 votes
private void _setCurrencyFormattingProperties(
  RequestContext context,
  NumberFormat numberFormatter
 )
{
  // Useless if... should be instanceof DecimalFormat
  // Potential ClassCastException before the change
  //if (numberFormatter instanceof NumberFormat)
  if (numberFormatter instanceof DecimalFormat)
  {
    DecimalFormat dfmt = (DecimalFormat)numberFormatter;
    DecimalFormatSymbols symbols = dfmt.getDecimalFormatSymbols();
    _setCurrencyInformation(context, symbols);
    dfmt.setDecimalFormatSymbols(symbols);
  }
  else
  {  //string cat at compile time.
    _LOG.warning("NUMBER_NOT_DECIMALFORMAT_IGNORE_CURRENCY");
  }
}
 
Example 3
Project: openjdk-jdk10   File: TestShrinkAuxiliaryData.java   Source Code and License Vote up 6 votes
private void printTestInfo(int maxCacheSize) {

        DecimalFormat grouped = new DecimalFormat("000,000");
        DecimalFormatSymbols formatSymbols = grouped.getDecimalFormatSymbols();
        formatSymbols.setGroupingSeparator(' ');
        grouped.setDecimalFormatSymbols(formatSymbols);

        System.out.format(
                "Test will use %s bytes of memory of %s available%n"
                + "Available memory is %s with %d bytes pointer size - can save %s pointers%n"
                + "Max cache size: 2^%d = %s elements%n",
                grouped.format(ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
                grouped.format(Runtime.getRuntime().maxMemory()),
                grouped.format(Runtime.getRuntime().maxMemory()
                        - ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
                Unsafe.ADDRESS_SIZE,
                grouped.format((Runtime.getRuntime().freeMemory()
                        - ShrinkAuxiliaryDataTest.getMemoryUsedByTest())
                        / Unsafe.ADDRESS_SIZE),
                maxCacheSize,
                grouped.format((int) Math.pow(2, maxCacheSize))
        );
    }
 
Example 4
Project: gdl2   File: DvQuantity.java   Source Code and License Vote up 5 votes
@Override
public String toString() {
    DecimalFormat format = new DecimalFormat();
    format.setMinimumFractionDigits(precision);
    format.setMaximumFractionDigits(precision);
    DecimalFormatSymbols dfs = format.getDecimalFormatSymbols();
    dfs.setDecimalSeparator(DECIMAL_SEPARATOR);
    format.setDecimalFormatSymbols(dfs);
    format.setGroupingUsed(false);
    return format.format(magnitude) + ((unit == null || unit.isEmpty()) ? "" : "," + getUnit());
}
 
Example 5
Project: creacoinj   File: BtcFormat.java   Source Code and License Vote up 5 votes
/** Set the currency symbol and international code of the underlying {@link
  * java.text.NumberFormat} object to the values of the last two arguments, respectively.
  * This method is invoked in the process of parsing, not formatting.
  *
  * Only invoke this from code synchronized on value of the first argument, and don't
  * forget to put the symbols back otherwise equals(), hashCode() and immutability will
  * break.  */
private static DecimalFormatSymbols setSymbolAndCode(DecimalFormat numberFormat, String symbol, String code) {
    checkState(Thread.holdsLock(numberFormat));
    DecimalFormatSymbols fs = numberFormat.getDecimalFormatSymbols();
    DecimalFormatSymbols ante = (DecimalFormatSymbols)fs.clone();
    fs.setInternationalCurrencySymbol(code);
    fs.setCurrencySymbol(symbol);
    numberFormat.setDecimalFormatSymbols(fs);
    return ante;
}
 
Example 6
Project: intellij-randomness   File: IntegerInsertAction.java   Source Code and License Vote up 5 votes
/**
 * Returns a nicely formatted representation of a long.
 *
 * @param integer a {@code long}
 * @return a nicely formatted representation of a long
 */
private String convertToString(final long integer) {
    final DecimalFormat format = new DecimalFormat();
    format.setGroupingUsed(integerSettings.getGroupingSeparator() != '\0');

    final DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
    symbols.setGroupingSeparator(integerSettings.getGroupingSeparator());
    format.setMinimumFractionDigits(0);
    format.setMaximumFractionDigits(0);
    format.setDecimalFormatSymbols(symbols);

    return format.format(integer);
}
 
Example 7
Project: intellij-randomness   File: DecimalInsertAction.java   Source Code and License Vote up 5 votes
/**
 * Returns a nicely formatted representation of a double.
 *
 * @param decimal a {@code double}
 * @return a nicely formatted representation of a double
 */
private String convertToString(final double decimal) {
    final DecimalFormat format = new DecimalFormat();
    format.setGroupingUsed(decimalSettings.getGroupingSeparator() != '\0');

    final DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
    symbols.setGroupingSeparator(decimalSettings.getGroupingSeparator());
    symbols.setDecimalSeparator(decimalSettings.getDecimalSeparator());
    format.setMinimumFractionDigits(decimalSettings.getDecimalCount());
    format.setMaximumFractionDigits(decimalSettings.getDecimalCount());
    format.setDecimalFormatSymbols(symbols);

    return format.format(decimal);
}
 
Example 8
Project: openjdk-jdk10   File: Helpers.java   Source Code and License Vote up 5 votes
/**
 * @return a number formatter instance which prints numbers in a human
 * readable form, like 9_223_372_036_854_775_807.
 */
public static NumberFormat numberFormatter() {
    DecimalFormat df = new DecimalFormat();
    DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
    dfs.setGroupingSeparator('_');
    dfs.setDecimalSeparator('.');
    df.setDecimalFormatSymbols(dfs);
    return df;
}
 
Example 9
Project: openjdk-jdk10   File: TestgetPatternSeparator_ja.java   Source Code and License Vote up 5 votes
public static void main(String[] argv) throws Exception {
    DecimalFormat df = (DecimalFormat)NumberFormat.getInstance(Locale.JAPAN);
    DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
    if (dfs.getPatternSeparator() != ';') {
        throw new Exception("DecimalFormatSymbols.getPatternSeparator doesn't return ';' in ja locale");
    }
}
 
Example 10
Project: okwallet   File: BtcFormat.java   Source Code and License Vote up 5 votes
/** Set the currency symbol and international code of the underlying {@link
  * java.text.NumberFormat} object to the values of the last two arguments, respectively.
  * This method is invoked in the process of parsing, not formatting.
  *
  * Only invoke this from code synchronized on value of the first argument, and don't
  * forget to put the symbols back otherwise equals(), hashCode() and immutability will
  * break.  */
private static DecimalFormatSymbols setSymbolAndCode(DecimalFormat numberFormat, String symbol, String code) {
    checkState(Thread.holdsLock(numberFormat));
    DecimalFormatSymbols fs = numberFormat.getDecimalFormatSymbols();
    DecimalFormatSymbols ante = (DecimalFormatSymbols)fs.clone();
    fs.setInternationalCurrencySymbol(code);
    fs.setCurrencySymbol(symbol);
    numberFormat.setDecimalFormatSymbols(fs);
    return ante;
}
 
Example 11
Project: myfaces-trinidad   File: NumberConverter.java   Source Code and License Vote up 4 votes
private NumberFormat _getNumberFormat(
  String pattern,
  String type,
  Locale locale,
  RequestContext reqCtx
  )
{
  NumberFormat nfmt;

  int formatType = _getType(pattern, type);

  nfmt = _getCachedNumberFormat(pattern, type, locale);

  if (nfmt == null)
  {
    nfmt = _getNumberFormatter(formatType, pattern, locale);

   _cacheNumberFormat(nfmt,pattern, type, locale);
  }

  if (nfmt instanceof DecimalFormat)
  {
    DecimalFormat dfmt = (DecimalFormat)nfmt;

    // what we get here is a shallow copy. cloned DFS
    DecimalFormatSymbols dfSymbols = dfmt.getDecimalFormatSymbols();

    _setUpDecimalSymbolFormatProperties(dfSymbols, reqCtx, locale);

    //since we get a shallow copy - setting it again after modification.
    ((DecimalFormat) nfmt).setDecimalFormatSymbols(dfSymbols);
  }
  else
  {
    if(_LOG.isWarning())
    {
      _LOG.warning("Failed to get hold of DecimalFormat for type: +" + type + "\n" +
                   "decimal separator,"         +
                   "number grouping separator," +
                   "currency code"              +
                   "will be defaulted based on locale " + locale.toString());
   }
  }
  return nfmt;
}
 
Example 12
Project: creacoinj   File: BtcFormat.java   Source Code and License Vote up 3 votes
/**
 * Set both the currency symbol and code of the underlying, mutable NumberFormat object
 * according to the given denominational units scale factor.  This is for formatting, not parsing.
 *
 * <p>Set back to zero when you're done formatting otherwise immutability, equals() and
 * hashCode() will break!
 *
 * @param scale Number of places the decimal point will be shifted when formatting
 *              a quantity of satoshis.
 * @return The DecimalFormatSymbols before changing
 */
protected static void prefixUnitsIndicator(DecimalFormat numberFormat, int scale) {
    checkState(Thread.holdsLock(numberFormat)); // make sure caller intends to reset before changing
    DecimalFormatSymbols fs = numberFormat.getDecimalFormatSymbols();
    setSymbolAndCode(numberFormat,
        prefixSymbol(fs.getCurrencySymbol(), scale), prefixCode(fs.getInternationalCurrencySymbol(), scale)
    );
}
 
Example 13
Project: okwallet   File: BtcFormat.java   Source Code and License Vote up 3 votes
/**
 * Set both the currency symbol and code of the underlying, mutable NumberFormat object
 * according to the given denominational units scale factor.  This is for formatting, not parsing.
 *
 * <p>Set back to zero when you're done formatting otherwise immutability, equals() and
 * hashCode() will break!
 *
 * @param scale Number of places the decimal point will be shifted when formatting
 *              a quantity of satoshis.
 */
protected static void prefixUnitsIndicator(DecimalFormat numberFormat, int scale) {
    checkState(Thread.holdsLock(numberFormat)); // make sure caller intends to reset before changing
    DecimalFormatSymbols fs = numberFormat.getDecimalFormatSymbols();
    setSymbolAndCode(numberFormat,
        prefixSymbol(fs.getCurrencySymbol(), scale), prefixCode(fs.getInternationalCurrencySymbol(), scale)
    );
}