下面列出了java.text.DecimalFormat#setDecimalSeparatorAlwaysShown ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
/**
* 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);
}
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);
}
public String toString()
{
DecimalFormat format =
(DecimalFormat)(NumberFormat.getInstance(Locale.ENGLISH));
format.setGroupingUsed(false);
format.setDecimalSeparatorAlwaysShown(false);
return format.format(m_value);
}
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;
}
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;
}
/**
* 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);
}
DoubleFormatter(String pattern) {
final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
format = new DecimalFormat(pattern, decimalFormatSymbols);
format.setParseIntegerOnly(false);
format.setParseBigDecimal(false);
format.setDecimalSeparatorAlwaysShown(true);
}
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);
}
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();
}
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);
}
private static String formatNumber(final double number, boolean commaSep) {
DecimalFormat f = new DecimalFormat(commaSep ? "#,###.###" : "###.###");
f.setDecimalSeparatorAlwaysShown(false);
return f.format(number);
}