本文整理汇总了Java中java.text.DecimalFormat.getInstance方法的典型用法代码示例。如果您正苦于以下问题:Java DecimalFormat.getInstance方法的具体用法?Java DecimalFormat.getInstance怎么用?Java DecimalFormat.getInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.text.DecimalFormat
的用法示例。
在下文中一共展示了DecimalFormat.getInstance方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: build
import java.text.DecimalFormat; //导入方法依赖的package包/类
private void build() {
try {
if (typeQualifier != null) {
buildQualifiedNumber();
return;
}
Number value;
if (number.contains(".")) {
DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US);
decimalFormat.setParseBigDecimal(true);
int decSymbolIndex = number.lastIndexOf(".");
if (decSymbolIndex > -1) {
int precision = number.substring(decSymbolIndex, number.length() - 1).length();
decimalFormat.setMaximumFractionDigits(precision);
}
value = decimalFormat.parse(number);
} else {
value = NumberFormat.getInstance(locale).parse(number);
}
set(value);
} catch (ParseException e) {
throw new RuntimeException(String.format("Invalid number '%s'", number));
}
}
示例2: getDecoratedStringFromNumber
import java.text.DecimalFormat; //导入方法依赖的package包/类
private String getDecoratedStringFromNumber(long number)
{
String numberPattern = "#,###,###,###";
String decoStr = "";
DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
if (_showCommas && !_showCurrency)
formatter.applyPattern(numberPattern);
else if (_showCommas && _showCurrency)
formatter.applyPattern(_currencySymbol + " " + numberPattern);
else if (!_showCommas && _showCurrency)
formatter.applyPattern(_currencySymbol + " ");
else if (!_showCommas && !_showCurrency)
{
decoStr = number + "";
return decoStr;
}
decoStr = formatter.format(number);
return decoStr;
}
示例3: valStr
import java.text.DecimalFormat; //导入方法依赖的package包/类
@Override
public String valStr() {
int pl = args.get(1).valInt().intValue();
if (pl < 0)
pl = 0;
String local = "en_US";
if (args.size() == 3)
local = args.get(2).valStr();
Locale loc = new Locale(local);
NumberFormat f = DecimalFormat.getInstance(loc);
if (args.get(0).isNull() || args.get(1).isNull()) {
this.nullValue = true;
return EMPTY;
}
BigDecimal bd = args.get(0).valDecimal();
BigDecimal bdnew = bd.setScale(pl, RoundingMode.HALF_UP);
return f.format(bdnew);
}
示例4: parse
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Convert the specified locale-sensitive input object into an output
* object of the specified type.
*
* @param value The input object to be converted
* @param pattern The pattern is used for the convertion
* @return The converted value
*
* @throws org.apache.commons.beanutils.ConversionException if conversion
* cannot be performed successfully
* @throws ParseException if an error occurs parsing a String to a Number
*/
@Override
protected Object parse(final Object value, final String pattern) throws ParseException {
if (value instanceof Number) {
return value;
}
// Note that despite the ambiguous "getInstance" name, and despite the
// fact that objects returned from this method have the same toString
// representation, each call to getInstance actually returns a new
// object.
final DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(locale);
// if some constructors default pattern to null, it makes only sense
// to handle null pattern gracefully
if (pattern != null) {
if (locPattern) {
formatter.applyLocalizedPattern(pattern);
} else {
formatter.applyPattern(pattern);
}
} else {
log.debug("No pattern provided, using default.");
}
return formatter.parse((String) value);
}
示例5: TrackPresenterImpl
import java.text.DecimalFormat; //导入方法依赖的package包/类
public TrackPresenterImpl(){
mDistanceFormater = (DecimalFormat) NumberFormat.getInstance();
mDistanceFormater.setMinimumFractionDigits(2);
mDistanceFormater.setMaximumFractionDigits(2);
mTimeFormater = (DecimalFormat) DecimalFormat.getInstance();
mTimeFormater.applyPattern("00");
}
示例6: assertThatMaxEntitiesExceeded
import java.text.DecimalFormat; //导入方法依赖的package包/类
private void assertThatMaxEntitiesExceeded(SAXParseException exception) {
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance();
df.applyPattern("##,###");
// Java7u21 and before
String maxEntitiesExceededMessage_le21 = df.format(64000);
// after Java7u21
String maxEntitiesExceededMessage_gt21 = "64000";
assertThat(
exception.getMessage(),
either(containsString(maxEntitiesExceededMessage_le21)).or(
containsString(maxEntitiesExceededMessage_gt21)));
}
示例7: chronoStringElapsedTime
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Retourne sous forme d'une chaîne de caractères String le temps écoulé depuis la mise
* à zéro du chronomètre interne.
*
* @return le nombre de secondes écoulés jusqu'au 1/1000s
*/
public static String chronoStringElapsedTime() {
Locale locale = Locale.getDefault();
DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(locale);
df.applyPattern("#,##0.000");
return df.format(chronoElapsedTime());
}
示例8: formatDecimal2String
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* 格式化数字 double2string
*/
private CharSequence formatDecimal2String(double decimal) {
String decimalScaleStr = "";
String s = mDecimalFill ? "0" : "#";
for (int i = 0; i < mDecimalScale; i++) {
decimalScaleStr += s;
}
DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
if (mShowCommas && !mShowSymbol) { // 显示数字分号 && 不显示数字符号
formatter.applyPattern(",##0." + decimalScaleStr);
} else if (mShowCommas) { // 显示数字分号 && 显示数字符号
formatter.applyPattern(mSymbol + ",##0." + decimalScaleStr);
} else if (mShowSymbol) { // 不显示数字分号 && 显示数字符号
formatter.applyPattern(mSymbol + "#0." + decimalScaleStr);
} else { // 不显示数字分号 && 不显示数字符号
formatter.applyPattern("#0." + decimalScaleStr);
}
formatter.setRoundingMode(RoundingMode.DOWN);
SpannableStringBuilder result = new SpannableStringBuilder(formatter.format(decimal));
if (mShowSymbol) {
if (mSymbolSize == 0) mSymbolSize = getTextSize();
result.setSpan(new AbsoluteSizeSpan((int) mSymbolSize), 0, mSymbol.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
return result;
}
示例9: formatDecimal2String
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* 格式化数字 double2string
*/
private CharSequence formatDecimal2String(double decimal) {
String decimalScaleStr = "";
String s = mDecimalFill ? "0" : "#";
for (int i = 0; i < mDecimalScale; i++) {
decimalScaleStr += s;
}
DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
if (mShowCommas && !mShowSymbol) { // 显示数字分号 && 不显示数字符号
formatter.applyPattern(",##0." + decimalScaleStr);
} else if (mShowCommas) { // 显示数字分号 && 显示数字符号
formatter.applyPattern(mSymbol + ",##0." + decimalScaleStr);
} else if (mShowSymbol) { // 不显示数字分号 && 显示数字符号
formatter.applyPattern(mSymbol + "#0." + decimalScaleStr);
} else { // 不显示数字分号 && 不显示数字符号
formatter.applyPattern("#0." + decimalScaleStr);
}
SpannableStringBuilder result = new SpannableStringBuilder(formatter.format(decimal));
if (mShowSymbol) {
if (mSymbolSize == 0) mSymbolSize = getTextSize();
result.setSpan(new AbsoluteSizeSpan((int) mSymbolSize), 0, mSymbol.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
return result;
}
示例10: formatToOneDecimalPlace
import java.text.DecimalFormat; //导入方法依赖的package包/类
public String formatToOneDecimalPlace(double value) {
NumberFormat format = DecimalFormat.getInstance();
format.setMinimumFractionDigits(1);
format.setMaximumFractionDigits(1);
return format.format(value);
}