下面列出了java.text.DecimalFormat#setRoundingMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void retryTransaction(TransactionResponse item) {
String transValue = "";
BigInteger value = new BigInteger(item.getValue().substring(2));
BigDecimal decimal = new BigDecimal(value);
BigDecimal formatted = Convert.fromWei(decimal, Convert.Unit.ETHER);
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setDecimalSeparator('.');
DecimalFormat decimalFormat = new DecimalFormat(ETH_SHOW_PATTERN, symbols);
decimalFormat.setRoundingMode(RoundingMode.DOWN);
transValue = decimalFormat.format(formatted);
Intent intent = new Intent(context, SendingCurrencyActivity.class);
intent.putExtra(Extras.WALLET_NUMBER, item.getTo());
intent.putExtra(Extras.AMOUNT_TO_SEND, transValue);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
@Override
protected void init(Bundle savedInstanceState) {
GuardaApp.getAppComponent().inject(this);
setToolBarTitle(getString(R.string.toolbar_title_confirm_coinify));
baseAmount = getIntent().getFloatExtra(Extras.COINIFY_IN_AMOUNT, 0.0f);
float amountWithCoinifyFee = 0.0f;
payMethod = getIntent().getStringExtra(Extras.COINIFY_PAY_METHOD);
fee.setText(String.format("%s %s" , getIntent().getStringExtra(Extras.COINIFY_EXCH_FEE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
til_coinify_method_fee.setHint(String.format("%s Order", getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
txfee.setText(String.format("%s %s", getIntent().getStringExtra(Extras.COINIFY_COINIFY_FEE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
coinify_method_fee.setText(String.format("%s %s", getIntent().getStringExtra(Extras.COINIFY_AMOUNT_RATE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
coinify_bank_chb_text.setText(R.string.coinify_sell_confirm);
btnNext.setEnabled(false);
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.HALF_EVEN);
send.setText(String.format("%s %s", getIntent().getFloatExtra(Extras.COINIFY_IN_AMOUNT, 0.0f), getIntent().getStringExtra(Extras.COINIFY_IN_AMOUNT_CUR)));
String tvapx = getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT);
received.setText(String.format("%s %s", tvapx, getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
email.setText(sharedManager.getCoinifyEmail());
coinify_bank_chb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
btnNext.setEnabled(true);
} else {
btnNext.setEnabled(false);
}
}
});
}
private void checkMultimediaFile(File file, boolean hasAudio, boolean hasVideo, double duration, String resolution,
String audioDecoder, String videoDecoder, boolean checkAudio) throws IOException {
// Check tracks, duration, resolution, framerate and decoders
MultimediaFileMetadata metadata = new MultimediaFileMetadata(file.getAbsolutePath());
if (hasVideo) {
if (checkAudio) {
if (hasAudio) {
Assert.assertTrue("Media file " + file.getAbsolutePath() + " should have audio",
metadata.hasAudio() && metadata.hasVideo());
Assert.assertTrue(metadata.getAudioDecoder().toLowerCase().contains(audioDecoder));
} else {
Assert.assertTrue("Media file " + file.getAbsolutePath() + " should have video",
metadata.hasVideo());
Assert.assertFalse(metadata.hasAudio());
}
}
if (resolution != null) {
Assert.assertEquals(resolution, metadata.getVideoWidth() + "x" + metadata.getVideoHeight());
}
Assert.assertTrue(metadata.getVideoDecoder().toLowerCase().contains(videoDecoder));
} else if (hasAudio && checkAudio) {
Assert.assertTrue(metadata.hasAudio());
Assert.assertFalse(metadata.hasVideo());
Assert.assertTrue(metadata.getAudioDecoder().toLowerCase().contains(audioDecoder));
} else {
Assert.fail("Cannot check a file witho no audio and no video");
}
// Check duration with 1 decimal precision
DecimalFormat df = new DecimalFormat("#0.0");
df.setRoundingMode(RoundingMode.UP);
log.info("Duration of {} according to ffmpeg: {} s", file.getName(), metadata.getDuration());
log.info("Duration of {} according to 'duration' property: {} s", file.getName(), duration);
log.info("Difference in s duration: {}", Math.abs(metadata.getDuration() - duration));
final double difference = 10;
Assert.assertTrue(
"Difference between recording entity duration (" + duration + ") and real video duration ("
+ metadata.getDuration() + ") is greater than " + difference + " in file " + file.getName(),
Math.abs((metadata.getDuration() - duration)) < difference);
}
public static double formatDouble2Db(double value, String format, RoundingMode mode) {
DecimalFormat df = getDecimal(format);
if (mode != null) {
df.setRoundingMode(mode);
}
return Double.parseDouble(df.format(value));
}
public static String getBtcFromLocalCurrency(String amount) {
ExchangeRates exchangeRates = ExchangeRates.getInstance();
// the following was losing precision at every toggling!!
//double newAmount = Double.parseDouble(currentAmount) * exchangeRates.getLocalToBtcRate();
double newAmount = Double.parseDouble(amount) / exchangeRates.getBtcToLocalRate();
DecimalFormat formatter = new DecimalFormat("#.########", DecimalFormatSymbols.getInstance( Locale.ENGLISH ));
formatter.setRoundingMode( RoundingMode.HALF_UP );
return formatter.format(newAmount);
}
public static String round(double value, int places) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < places; i++) stringBuilder.append("#");
DecimalFormat df = new DecimalFormat("#." + stringBuilder.toString());
df.setRoundingMode(RoundingMode.CEILING);
return df.format(value);
}
@OnClick(R.id.btn_max)
public void maxAmount(View view) {
if (balance != null && !balance.equals(new BigDecimal(0))) {
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat(ETH_SHOW_PATTERN, symbols);
decimalFormat.setRoundingMode(RoundingMode.FLOOR);
String clearAmount = decimalFormat.format(balance).replace(",","");
etAmountToSend.setText(clearAmount);
inputLayout.setCurrentText(clearAmount);
etAmountToSend.setSelection(etAmountToSend.getText().length());
}
}
public static String getEthStringSzabo(BigInteger szabo)
{
BigDecimal ethPrice = fromWei(toWei(new BigDecimal(szabo), Unit.SZABO), Unit.ETHER);
DecimalFormat df = new DecimalFormat("0.#####");
df.setRoundingMode(RoundingMode.CEILING);
return df.format(ethPrice);
}
@Nullable
protected static String getFStopDescription(double fStop)
{
DecimalFormat format = new DecimalFormat("0.0");
format.setRoundingMode(RoundingMode.HALF_UP);
return "f/" + format.format(fStop);
}
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);
}
public static DecimalFormat getDecimalFormat(int floatPrecision) {
DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);
symbols.setInfinity("Infinity");
symbols.setNaN("NaN");
df.setDecimalFormatSymbols(symbols);
df.setGroupingUsed(false);
df.setRoundingMode(RoundingMode.HALF_EVEN);
df.setMaximumFractionDigits(floatPrecision);
return df;
}
@Override
protected void init(Bundle savedInstanceState) {
GuardaApp.getAppComponent().inject(this);
setToolBarTitle(getString(R.string.toolbar_title_confirm_coinify));
baseAmount = getIntent().getFloatExtra(Extras.COINIFY_IN_AMOUNT, 0.0f);
float amountWithCoinifyFee = 0.0f;
payMethod = getIntent().getStringExtra(Extras.COINIFY_PAY_METHOD);
fee.setText(String.format("%s %s" , getIntent().getStringExtra(Extras.COINIFY_EXCH_FEE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
til_coinify_method_fee.setHint(String.format("%s Order", getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
txfee.setText(String.format("%s %s", getIntent().getStringExtra(Extras.COINIFY_COINIFY_FEE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
coinify_method_fee.setText(String.format("%s %s", getIntent().getStringExtra(Extras.COINIFY_AMOUNT_RATE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
coinify_bank_chb_text.setText(R.string.coinify_sell_confirm);
btnNext.setEnabled(false);
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.HALF_EVEN);
send.setText(String.format("%s %s", getIntent().getFloatExtra(Extras.COINIFY_IN_AMOUNT, 0.0f), getIntent().getStringExtra(Extras.COINIFY_IN_AMOUNT_CUR)));
String tvapx = getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT);
received.setText(String.format("%s %s", tvapx, getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
email.setText(sharedManager.getCoinifyEmail());
coinify_bank_chb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
btnNext.setEnabled(true);
} else {
btnNext.setEnabled(false);
}
}
});
}
@Override
protected void init(Bundle savedInstanceState) {
GuardaApp.getAppComponent().inject(this);
setToolBarTitle(getString(R.string.toolbar_title_confirm_coinify));
baseAmount = getIntent().getFloatExtra(Extras.COINIFY_IN_AMOUNT, 0.0f);
float amountWithCoinifyFee = 0.0f;
payMethod = getIntent().getStringExtra(Extras.COINIFY_PAY_METHOD);
fee.setText(String.format("%s %s" , getIntent().getStringExtra(Extras.COINIFY_EXCH_FEE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
til_coinify_method_fee.setHint(String.format("%s Order", getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
txfee.setText(String.format("%s %s", getIntent().getStringExtra(Extras.COINIFY_COINIFY_FEE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
coinify_method_fee.setText(String.format("%s %s", getIntent().getStringExtra(Extras.COINIFY_AMOUNT_RATE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
coinify_bank_chb_text.setText(R.string.coinify_sell_confirm);
btnNext.setEnabled(false);
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.HALF_EVEN);
send.setText(String.format("%s %s", getIntent().getFloatExtra(Extras.COINIFY_IN_AMOUNT, 0.0f), getIntent().getStringExtra(Extras.COINIFY_IN_AMOUNT_CUR)));
String tvapx = getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT);
received.setText(String.format("%s %s", tvapx, getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
email.setText(sharedManager.getCoinifyEmail());
coinify_bank_chb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
btnNext.setEnabled(true);
} else {
btnNext.setEnabled(false);
}
}
});
}
private void updateTokenBalance(){
balance = tokenManager.getTokenByCode(tokenCode).getTokenNum();
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
DecimalFormat decimalFormat = new DecimalFormat(ETH_SHOW_PATTERN, symbols);
decimalFormat.setRoundingMode(RoundingMode.DOWN);
if (balance != null && !balance.equals(new BigDecimal(0))) {
setCurrentBalance(decimalFormat.format(balance), tokenCode.toUpperCase());
}
}
@Override
protected void init(Bundle savedInstanceState) {
GuardaApp.getAppComponent().inject(this);
setToolBarTitle(getString(R.string.toolbar_title_confirm_coinify));
baseAmount = getIntent().getFloatExtra(Extras.COINIFY_IN_AMOUNT, 0.0f);
float amountWithCoinifyFee = 0.0f;
payMethod = getIntent().getStringExtra(Extras.COINIFY_PAY_METHOD);
fee.setText(String.format("%s %s" , getIntent().getStringExtra(Extras.COINIFY_EXCH_FEE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
til_coinify_method_fee.setHint(String.format("%s Order", getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
txfee.setText(String.format("%s %s", getIntent().getStringExtra(Extras.COINIFY_COINIFY_FEE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
coinify_method_fee.setText(String.format("%s %s", getIntent().getStringExtra(Extras.COINIFY_AMOUNT_RATE), getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
coinify_bank_chb_text.setText(R.string.coinify_sell_confirm);
btnNext.setEnabled(false);
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.HALF_EVEN);
send.setText(String.format("%s %s", getIntent().getFloatExtra(Extras.COINIFY_IN_AMOUNT, 0.0f), getIntent().getStringExtra(Extras.COINIFY_IN_AMOUNT_CUR)));
String tvapx = getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT);
received.setText(String.format("%s %s", tvapx, getIntent().getStringExtra(Extras.COINIFY_OUT_AMOUNT_CUR)));
email.setText(sharedManager.getCoinifyEmail());
coinify_bank_chb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
btnNext.setEnabled(true);
} else {
btnNext.setEnabled(false);
}
}
});
}
private String balanceToString(BigDecimal balance) {
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setDecimalSeparator('.');
DecimalFormat decimalFormat = new DecimalFormat(ETH_SHOW_PATTERN, symbols);
decimalFormat.setRoundingMode(RoundingMode.DOWN);
if (!balance.equals(new BigDecimal(0))) {
return decimalFormat.format(balance);
} else {
return "00.00";
}
}
public static String float2Str(float value, String format, RoundingMode mode) {
DecimalFormat decimalFormat = getDecimal(format);
if (mode != null) {
decimalFormat.setRoundingMode(mode);
}
return decimalFormat.format(value);
}
@Override
public void onBindViewHolder(TransHistoryItemHolder holder, final int position) {
final TransactionResponse item = getTxByPosition(position);
holder.rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.OnItemClick(position);
}
}
});
if (item.isError()) {
// enableRetryButton(holder);
holder.viewIndicator.setBackground(view.getContext().getResources().getDrawable(R.drawable.transaction_indicator_red));
holder.viewIndicator.setVisibility(View.VISIBLE);
holder.tvTxStatus.setTextColor(view.getContext().getResources().getColor(R.color.txStatusRed));
holder.tvTxStatus.setText(R.string.tx_status_fail);
// holder.viewIndicator.setVisibility(View.INVISIBLE);
// holder.ivRetry.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// retryTransaction(item);
// }
// });
} else if (item.getConfirmations() == null) {
// enableLoader(holder);
holder.tvTxStatus.setTextColor(view.getContext().getResources().getColor(R.color.txStatusGrey));
holder.tvTxStatus.setText(R.string.tx_status_node);
// holder.viewIndicator.setVisibility(View.INVISIBLE);
if (repeatHandler == null || repeatHandler.isInterrupted()){
// startUpdateTask();
}
} else if (Long.parseLong(item.getConfirmations()) < MIN_CONFIRMATIONS) {
// starCheckTxTask(item.getHash());
// enableLoader(holder);
holder.tvTxStatus.setText(R.string.tx_status_wait);
// holder.viewIndicator.setVisibility(View.INVISIBLE);
} else {
// disableLoader(holder);
holder.tvTxStatus.setVisibility(View.GONE);
holder.viewIndicator.setBackground(view.getContext().getResources().getDrawable(R.drawable.transaction_indicator_green));
holder.viewIndicator.setVisibility(View.VISIBLE);
}
BigInteger value = new BigInteger(item.getValue());
BigDecimal decimal = new BigDecimal(value);
BigDecimal formatted = new BigDecimal(WalletAPI.satoshiToCoinsString(decimal));
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setDecimalSeparator('.');
DecimalFormat decimalFormat = new DecimalFormat(ETH_SHOW_PATTERN, symbols);
decimalFormat.setRoundingMode(RoundingMode.DOWN);
String valueStr = (isDebit(walletManager.getWalletFriendlyAddress(), item.getTo()) ? "+" : "-")
+ " " + decimalFormat.format(formatted);
holder.tvTransactionSum.setText(valueStr);
// if (isDebit(walletManager.getWalletFriendlyAddress(), item.getTo())) {
// holder.viewIndicator.setBackground(view.getContext().getResources().getDrawable(R.drawable.transaction_indicator_green));
// } else {
// holder.viewIndicator.setBackground(view.getContext().getResources().getDrawable(R.drawable.transaction_indicator_red));
// }
holder.tvDate.setText(CalendarHelper.parseDateToddMMyyyy(item.getTimeStamp() * 1000));
}
public static String truncateWithDecimalFormat(double d) {
DecimalFormat df = new DecimalFormat("#,###");
df.setRoundingMode(RoundingMode.FLOOR);
return df.format(d);
}
@Test
public void format_decimal_with_rounding_mode () {
double hdTv = 1229.99;
DecimalFormat df = new DecimalFormat("###,###");
df.setRoundingMode(RoundingMode.HALF_UP);
assertEquals("1,230", df.format(hdTv));
}