下面列出了java.text.DecimalFormat#getDecimalFormatSymbols ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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))
);
}
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))
);
}
private static void updateExcelDecimalSeparator( DecimalFormat numberFormat )
{
DecimalFormatSymbols symbol = numberFormat.getDecimalFormatSymbols( );
if ( symbol.getDecimalSeparator( ) != EXCEL_DECIMAL_SEPARATOR )
{
symbol.setDecimalSeparator( EXCEL_DECIMAL_SEPARATOR );
numberFormat.setDecimalFormatSymbols( symbol );
}
}
public void test_exponentSeparator() throws Exception {
DecimalFormat df = new DecimalFormat("0E0");
assertEquals("1E4", df.format(12345.));
DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
dfs.setExponentSeparator("-useless-api-");
df.setDecimalFormatSymbols(dfs);
assertEquals("1-useless-api-4", df.format(12345.));
}
/** 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;
}
private UserSessionLocale(final String adLanguage)
{
final Language language = Language.getLanguage(adLanguage);
if (language == null)
{
throw new IllegalArgumentException("No language found for " + adLanguage);
}
this.adLanguage = language.getAD_Language();
final DecimalFormat decimalFormat = DisplayType.getNumberFormat(DisplayType.Amount, language);
final DecimalFormatSymbols decimalFormatSymbols = decimalFormat.getDecimalFormatSymbols();
numberDecimalSeparator = decimalFormatSymbols.getDecimalSeparator();
numberGroupingSeparator = decimalFormatSymbols.getGroupingSeparator();
}
@Override
public String toString() {
DecimalFormat formatter = new DecimalFormat("##0.####");
DecimalFormatSymbols dfs = formatter.getDecimalFormatSymbols();
dfs.setDecimalSeparator('.');
formatter.setDecimalFormatSymbols(dfs);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.x.length; i++) {
sb.append(formatter.format(x[i]));
sb.append(" ");
sb.append(formatter.format(y[i]));
sb.append(" ");
}
return sb.toString();
}
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");
}
}
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");
}
}
/**
* @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;
}
public static char getGroupingSeparator(Locale locale) {
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(locale);
DecimalFormatSymbols decimalFormatSymbols = df.getDecimalFormatSymbols();
logger.debug("IN");
char thousands = decimalFormatSymbols.getGroupingSeparator();
logger.debug("OUT");
return thousands;
}
public MarvinMatrixPanel(int rows, int columns){
this.rows = rows;
this.columns = columns;
JPanel panelTextFields = new JPanel();
GridLayout layout = new GridLayout(rows, columns);
panelTextFields.setLayout(layout);
//Format
DecimalFormat decimalFormat = new DecimalFormat();
DecimalFormatSymbols dfs = decimalFormat.getDecimalFormatSymbols();
dfs.setDecimalSeparator('.');
decimalFormat.setDecimalFormatSymbols(dfs);
textFields = new JFormattedTextField[rows][columns];
for(int r=0; r<rows; r++){
for(int c=0; c<columns; c++){
textFields[r][c] = new JFormattedTextField(decimalFormat);
textFields[r][c].setValue(0.0);
textFields[r][c].setColumns(4);
panelTextFields.add(textFields[r][c]);
}
}
setLayout(new FlowLayout(FlowLayout.CENTER));
add(panelTextFields);
}
/**
* Returns a thousans separated decimar formatter.
*
* @return
*/
public static DecimalFormat getThousandSeperatedDecimalFormat() {
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = df.getDecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
df.setDecimalFormatSymbols(symbols);
return df;
}
public static void localeDecimalInput(final EditText editText) {
final DecimalFormat decFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
final DecimalFormatSymbols symbols=decFormat.getDecimalFormatSymbols();
final String defaultSeparator = Character.toString(symbols.getDecimalSeparator());
final String otherSeparator = ".".equals(defaultSeparator) ? "," : ".";
editText.setHint(String.format("0%s00",defaultSeparator));
editText.addTextChangedListener(new TextWatcher() {
private boolean isEditing =false;
@Override
public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
Log.d(TAG,s + " " + start + " " + before + " " + count);
}
@Override
public void afterTextChanged(Editable editable) {
if (isEditing)
return;
isEditing = true;
final int index = editable.toString().indexOf(otherSeparator);
if (index > 0)
editable.replace(index,index+1, defaultSeparator);
if (editable.toString().contains(".") || editable.toString().contains(","))
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
else
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789.,"));
isEditing =false;
}
});
}
/** 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;
}
public static Currency fromConfig(ConfigurationSection config) {
DecimalFormat format = new DecimalFormat(config.getString("format", "0.00"));
if (config.getInt("grouping", 0) > 0) {
DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
if (symbols.getDecimalSeparator() == ',') { // French
symbols.setGroupingSeparator(' ');
} else {
symbols.setGroupingSeparator(',');
}
String groupingSeparator = config.getString("grouping-separator", null);
if (!Strings.isNullOrEmpty(groupingSeparator)) {
symbols.setGroupingSeparator(groupingSeparator.charAt(0));
}
String decimalSeparator = config.getString("decimal-separator", ".");
if (!Strings.isNullOrEmpty(decimalSeparator)) {
symbols.setDecimalSeparator(decimalSeparator.charAt(0));
}
format.setDecimalFormatSymbols(symbols);
format.setGroupingUsed(true);
format.setGroupingSize(3);
}
return new Currency(
config.getString("name.singular", "dollar"),
config.getString("name.plural", "dollars"),
format,
config.getString("balance-format", "{1} {2}")
);
}
private static char getSeparator() {
DecimalFormat format= (DecimalFormat) DecimalFormat.getInstance();
DecimalFormatSymbols symbols=format.getDecimalFormatSymbols();
char sep=symbols.getDecimalSeparator();
return sep;
}
private boolean hasMultipleDecimalSeparators(String string, DecimalFormat df) {
DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
String patternNonDecimalSeparators = "[^" + dfs.getDecimalSeparator() + "]";
String decimalSeparatorsLeftOver = string.replaceAll(patternNonDecimalSeparators, "");
return (decimalSeparatorsLeftOver.length() > 1);
}
/**
* 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>
* <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)
);
}
/**
* Determines whether the specified char is a number
*
* @param c The char
* @return If the char is a digit or a decimal separator
*/
public static boolean isNumber(char c) {
DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance();
DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
return Character.isDigit(c) || symbols.getDecimalSeparator() == c;
}