本文整理汇总了Java中java.text.DecimalFormat.setRoundingMode方法的典型用法代码示例。如果您正苦于以下问题:Java DecimalFormat.setRoundingMode方法的具体用法?Java DecimalFormat.setRoundingMode怎么用?Java DecimalFormat.setRoundingMode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.text.DecimalFormat
的用法示例。
在下文中一共展示了DecimalFormat.setRoundingMode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: displayProbabilities
import java.text.DecimalFormat; //导入方法依赖的package包/类
private void displayProbabilities() {
final DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
final DecimalFormat df = new DecimalFormat("", symbols);
df.setMaximumFractionDigits(1);
df.setRoundingMode(RoundingMode.HALF_UP);
df.setMinimumIntegerDigits(1);
Map<HiddenPower, Double> hiddenPowers = hiddenPowerCalculator.computeHiddenPower(pokemon);
double badHiddenPower = 0;
for (final HiddenPower hiddenPower: HiddenPower.values()) {
double probability = hiddenPowers.get(hiddenPower);
hiddenPowerLabels.get(hiddenPower.ordinal()).setText(hiddenPower.getName() + ": " + df.format(probability * 100) + "%");
if (hiddenPower.equals(HiddenPower.WATER) || hiddenPower.equals(HiddenPower.GRASS)) {
badHiddenPower+= probability;
}
}
feelsBadMan.setVisible(badHiddenPower >= 0.375);
}
示例2: getWeatherResponse
import java.text.DecimalFormat; //导入方法依赖的package包/类
@Override
public void getWeatherResponse(WeatherResponse weatherResponse) {
if (weatherResponse.getWeatherItems() != null) {
setWeatherWidgetImage(weatherResponse.getWeatherItems()[0].getIcon());
weatherTextView.setText(weatherResponse.getWeatherItems()[0].getDescription());
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
double currentTemp = weatherResponse.getTemperatureItem().getTemp();
double maxTemp = weatherResponse.getTemperatureItem().getTempMax();
double minTemp = weatherResponse.getTemperatureItem().getTempMin();
if (sharedPreferences.getString("degrees_list", "ºC").equalsIgnoreCase("ºF")) {
currentTemp = (currentTemp * 1.8) + 32;
maxTemp = (maxTemp * 1.8) + 32;
minTemp = (minTemp * 1.8) + 32;
}
DecimalFormat df = new DecimalFormat("#.#");
df.setRoundingMode(RoundingMode.CEILING);
tempTextView.setText(df.format(currentTemp) + sharedPreferences.getString("degrees_list", "ºC"));
tempMaxTextView.setText(df.format(maxTemp) + sharedPreferences.getString("degrees_list", "ºC"));
tempMinTextView.setText(df.format(minTemp) + sharedPreferences.getString("degrees_list", "ºC"));
} else {
weatherRelativeLayout.setVisibility(View.GONE);
}
}
示例3: toString
import java.text.DecimalFormat; //导入方法依赖的package包/类
public String toString(Object obj){
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ROOT);
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
DecimalFormat formatDecimal = new DecimalFormat("#0.00", symbols);
formatDecimal.setRoundingMode(RoundingMode.HALF_UP);
return formatDecimal.format((Double) obj);
}
示例4: toString
import java.text.DecimalFormat; //导入方法依赖的package包/类
@Override
public String toString(Disk disk) {
if (disk == null) {
return "";
}
double bytesInGB = disk.getSize() / 1073741824.0;
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.CEILING);
return disk.getName() + ": " + disk.model + " [" + df.format(bytesInGB) + "GB]";
}
示例5: format
import java.text.DecimalFormat; //导入方法依赖的package包/类
public static String format(Number number, String prefix, String suffix, int numFractionDigits, boolean grouping){
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(numFractionDigits);
df.setMinimumFractionDigits(numFractionDigits);
df.setRoundingMode(RoundingMode.HALF_UP);
df.setGroupingUsed(grouping);
df.setPositivePrefix(prefix);
df.setNegativePrefix(prefix + "-");
df.setPositiveSuffix(suffix);
df.setNegativeSuffix(suffix);
return df.format(number);
}
示例6: getNumberFormat
import java.text.DecimalFormat; //导入方法依赖的package包/类
@Override
protected NumberFormat getNumberFormat(Locale locale) {
DecimalFormat format = (DecimalFormat) NumberFormat.getCurrencyInstance(locale);
format.setParseBigDecimal(true);
format.setMaximumFractionDigits(this.fractionDigits);
format.setMinimumFractionDigits(this.fractionDigits);
if (this.roundingMode != null && roundingModeOnDecimalFormat) {
format.setRoundingMode(this.roundingMode);
}
if (this.currency != null) {
format.setCurrency(this.currency);
}
return format;
}
示例7: test
import java.text.DecimalFormat; //导入方法依赖的package包/类
public static <T extends TableBlock> String test(Collection<T> tableBlocks,int testCount)
{
Random seed=new Random();
String result="";
int allCount=testCount;
Map<T,Integer> map=new HashMap<T, Integer>();
//统计元素出现次数
for(int i=0;i<allCount;i++)
{
T item=randomBlock(tableBlocks, seed);
if(map.containsKey(item))
{
int tmp=map.get(item);
tmp++;
map.put(item, tmp);
}else
{
map.put(item, 1);
}
}
//计算概率
for(T key:map.keySet())
{
int count=map.get(key);
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(2);// 设置精确2位小数
df.setRoundingMode(RoundingMode.HALF_UP); //模式 例如四舍五入
double p = (double)count/(double)allCount*100;//以100为计算概率200为实际总概率,则计算的概率会减半
result="元素:"+key+"出现次数"+count+"/"+allCount+",出现概率:"+df.format(p)+"%";
System.out.println("元素:"+key+"出现次数"+count+"/"+allCount+",出现概率:"+df.format(p)+"%");
}
return result;
}
示例8: formatPercentage
import java.text.DecimalFormat; //导入方法依赖的package包/类
public static String formatPercentage(double d) {
// Defining that our percentage is precise down to 0.1%
DecimalFormat df = new DecimalFormat("0.0");
// Protecting this method against non-US locales that would not use '.' as decimal separation
DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
decimalFormatSymbols.setDecimalSeparator('.');
df.setDecimalFormatSymbols(decimalFormatSymbols);
// Making sure that we round the 0.1% properly out of the double value
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(d);
}
示例9: formatDays
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Rounds <code>days</code> to <code>numDecimals</code> decimals and appends the pluralised "days" string.
*
* @param days The number of days to format.
* @param context The application context.
* @param quantityId The ID of the pluralised "days" string.
* @param numDecimals The maximum number of decimals to use. If more would be needed, the days will be rounded.
* @return The string representation.
*/
public static String formatDays(float days, Context context, int quantityId, int numDecimals) {
StringBuilder builder = new StringBuilder(PATTERN_BASE + ".");
for (int i = 0; i < numDecimals; ++i) {
builder.append("#");
}
DecimalFormat decimalFormat = new DecimalFormat(builder.toString());
int quantity = Math.round(days * 10.0f) == 10 ? 1 : 2;
decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
return decimalFormat.format(days) + '\u00A0' + context.getResources().getQuantityText(quantityId, quantity);
}
示例10: 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;
}
示例11: arredondarDuasCasas
import java.text.DecimalFormat; //导入方法依赖的package包/类
public static Double arredondarDuasCasas(Double valor){
DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
DecimalFormat df = new DecimalFormat("###.##", symbols);
df.setRoundingMode(RoundingMode.HALF_EVEN);
return Double.valueOf(df.format(valor));
}
示例12: doubleNumberFormat
import java.text.DecimalFormat; //导入方法依赖的package包/类
public String doubleNumberFormat(double number) {
DecimalFormat format = new DecimalFormat("###########0.00");
format.setRoundingMode(RoundingMode.FLOOR);
return format.format(number);
}
示例13: updateScreenState
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* update the the lower gui section with the Values from an AnswerScreenState
* @param state
* history: if true, do not update the title
*/
public void updateScreenState(final AnswerScreenState state, final boolean history){
DecimalFormat df = new DecimalFormat("#.###");
df.setRoundingMode(RoundingMode.CEILING);
final String ampFactor = df.format(state.ampFactor);
Runnable guiUpdate = new Runnable() {
@Override
public void run() {
if (!history){
setTitle("");
}
((TextView) findViewById(R.id.txtStatusText)).setText(state.status);
((TextView) findViewById(R.id.txtServerIP)).setText(state.server);
((TextView) findViewById(R.id.txtQbytes)).setText(""+state.qsize);
((TextView) findViewById(R.id.txtAbytes)).setText(""+state.asize);
((TextView) findViewById(R.id.txtAmpfactor)).setText(ampFactor);
((EditText) findViewById(R.id.txtResult)).setText(state.answerText);
if (state.rcode>-1) {
((TextView) findViewById(R.id.txtRcode)).setText(Rcode.string(state.rcode));
} else {
((TextView) findViewById(R.id.txtRcode)).setText("");
}
((CheckBox) findViewById(R.id.cbaAA)).setChecked( state.flag_AA);
((CheckBox) findViewById(R.id.cbaTC)).setChecked( state.flag_TC);
((CheckBox) findViewById(R.id.cbaRD)).setChecked( state.flag_RD);
((CheckBox) findViewById(R.id.cbaRA)).setChecked( state.flag_RA);
((CheckBox) findViewById(R.id.cbaAD)).setChecked( state.flag_AD);
((CheckBox) findViewById(R.id.cbaCD)).setChecked( state.flag_CD);
}
};
runOnUiThread(guiUpdate);
}
示例14: formatShow
import java.text.DecimalFormat; //导入方法依赖的package包/类
private static String formatShow(BigDecimal value) {
DecimalFormat percentFormat = new DecimalFormat(MASK);
percentFormat.setDecimalSeparatorAlwaysShown(false);
percentFormat.setRoundingMode(RoundingMode.DOWN);
return percentFormat.format(value).replace(',', '.');
}
示例15: roundValue
import java.text.DecimalFormat; //导入方法依赖的package包/类
/**
* Rounds a value by using the specified scale with half up rounding and
* without grouping. Even if the last part of the decimal value is 0 in
* specified fraction digits, always all decimal places are filled with 0.
* (e.g. value 1.2, 5 fraction digits, en locale => '1.20000' will be
* returned)
*
* @param value
* Value to be formatted.
* @param locale
* Current user's locale for formatting.
* @param fractionDigits
* Number of decimal places.
* @return
*/
public static String roundValue(BigDecimal value, Locale locale,
int fractionDigits) {
DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
df.setGroupingUsed(false);
df.setMaximumFractionDigits(fractionDigits);
df.setMinimumFractionDigits(fractionDigits);
df.setRoundingMode(RoundingMode.HALF_UP);
return df.format(value);
}