下面列出了java.text.DecimalFormatSymbols#setDecimalSeparator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public BigDecimal getAmount() {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(' ');
DecimalFormat df = new DecimalFormat("#,###.##");
df.setDecimalSeparatorAlwaysShown(true);
df.setDecimalFormatSymbols(symbols);
df.setParseBigDecimal(true);
try {
String s = edAmount.getText().toString();
if (s.equals("")) {
s = "0";
}
return ((BigDecimal) df.parse(s.replaceAll(" ", ""))).setScale(2, RoundingMode.HALF_UP);
} catch (NumberFormatException | ParseException nfe) {
return BigDecimal.ZERO;
}
}
/**
* creates a decimal formatter from the server message
*
* @param reader reader of the message coming from the server
* @throws OLcRemoteException if anything bad happens on the server
* @throws IOException if a problem happens during the message
* transmission
*/
public CDecimalFormatter(MessageReader reader) throws OLcRemoteException, IOException {
reader.returnNextStartStructure("DEF");
minimum = reader.returnNextDecimalField("MIN");
maximum = reader.returnNextDecimalField("MAX");
linear = reader.returnNextBooleanField("LIN");
colorscheme = reader.returnNextBooleanField("CLS");
reader.returnNextEndStructure("DEF");
String formatfordecimalstring = "###,###.###";
formatfordecimal = new DecimalFormat(formatfordecimalstring);
DecimalFormatSymbols formatforsymbol = formatfordecimal.getDecimalFormatSymbols();
formatforsymbol.setGroupingSeparator(' ');
formatforsymbol.setDecimalSeparator('.');
formatfordecimal.setDecimalFormatSymbols(formatforsymbol);
formatfordecimal.setParseBigDecimal(true);
}
public FormatStrings(char decimalSeparator, char groupingSeparator,
String integerFormat, String doubleFormat, String decimalFormat,
String dateFormat, String dateTimeFormat, String offsetDateTimeFormat,
String timeFormat, String offsetTimeFormat,
String trueString, String falseString) {
formatSymbols = new DecimalFormatSymbols();
formatSymbols.setDecimalSeparator(decimalSeparator);
formatSymbols.setGroupingSeparator(groupingSeparator);
this.integerFormat = integerFormat;
this.doubleFormat = doubleFormat;
this.decimalFormat = decimalFormat;
this.dateFormat = dateFormat;
this.dateTimeFormat = dateTimeFormat;
this.offsetDateTimeFormat = offsetDateTimeFormat;
this.timeFormat = timeFormat;
this.offsetTimeFormat = offsetTimeFormat;
this.trueString = trueString;
this.falseString = falseString;
}
/**
* Process a currency definition.
*
* @param row record from XER file
*/
private void processCurrency(Row row)
{
String currencyName = row.getString("curr_short_name");
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(row.getString("decimal_symbol").charAt(0));
symbols.setGroupingSeparator(row.getString("digit_group_symbol").charAt(0));
DecimalFormat nf = new DecimalFormat();
nf.setDecimalFormatSymbols(symbols);
nf.applyPattern("#.#");
m_currencyMap.put(currencyName, nf);
if (currencyName.equalsIgnoreCase(m_defaultCurrencyName))
{
m_numberFormat = nf;
m_defaultCurrencyData = row;
}
}
private void retryTransaction(TransactionResponse item) {
String transValue = "";
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);
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);
}
/**
* Set double value in specified field, with given number of digits before
* and after the decimal separator ('.'). When necessary, the value is
* padded with leading zeros and/or rounded to meet the requested number of
* digits.
*
* @param index Field index
* @param value Value to set
* @param leading Number of digits before decimal separator
* @param decimals Maximum number of digits after decimal separator
* @see #setDoubleValue(int, double)
*/
protected final void setDoubleValue(int index, double value, int leading,
int decimals) {
StringBuilder pattern = new StringBuilder();
for (int i = 0; i < leading; i++) {
pattern.append('0');
}
if (decimals > 0) {
pattern.append('.');
for (int i = 0; i < decimals; i++) {
pattern.append('0');
}
}
if (pattern.length() == 0) {
pattern.append('0');
}
DecimalFormat nf = new DecimalFormat(pattern.toString());
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
nf.setDecimalFormatSymbols(dfs);
setStringValue(index, nf.format(value));
}
private void updateArrivalField() {
if (amountToSend == null) return;
if (isInclude) {
BigDecimal amountDecimal = new BigDecimal(amountToSend);
BigDecimal arrivalAmountDecimal = amountDecimal.subtract(currentFeeEth);
if (arrivalAmountDecimal.compareTo(BigDecimal.ZERO) < 0) {
arrivalAmountDecimal = BigDecimal.ZERO;
}
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
symbols.setDecimalSeparator('.');
DecimalFormat decimalFormat = new DecimalFormat("###0.########", symbols);
arrivalAmountToSend = decimalFormat.format(arrivalAmountDecimal);
etArrivalAmount.setText(arrivalAmountToSend);
} else {
etArrivalAmount.setText(amountToSend);
}
}
/**
* Gets a decimal format that with the desired number of decimal places.
*
* @param decimalPlaces The number of decimal places.
* @return The desired decimal format.
*/
public static DecimalFormat getDecimalFormat(final int decimalPlaces) {
if (decimalPlaces < 0) {
throw new IllegalArgumentException("decimalPlaces must be non-negative");
}
final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
decimalFormatSymbols.setDecimalSeparator('.');
final StringBuilder builder = new StringBuilder();
builder.append("#0");
if (decimalPlaces > 0) {
builder.append('.');
final char[] zeros = new char[decimalPlaces];
Arrays.fill(zeros, '0');
builder.append(zeros);
}
final DecimalFormat format = new DecimalFormat(builder.toString(), decimalFormatSymbols);
format.setGroupingUsed(false);
return format;
}
/**
* Open Lowcode uses as default a number formatting that is non ambiguous for
* both English speaking and French speaking countries. Decimal separator is dot
* '.', thousands grouping separator is space ' '.
*
* @return
*/
public static DecimalFormat getOLcDecimalFormatter() {
String formatfordecimalstring = "###,###.###";
DecimalFormat formatfordecimal = new DecimalFormat(formatfordecimalstring);
DecimalFormatSymbols formatforsymbol = formatfordecimal.getDecimalFormatSymbols();
formatforsymbol.setGroupingSeparator(' ');
formatforsymbol.setDecimalSeparator('.');
formatfordecimal.setDecimalFormatSymbols(formatforsymbol);
formatfordecimal.setParseBigDecimal(true);
return formatfordecimal;
}
/**
* Creates a new formatter.
* @param decimals the max number of decimal places
*/
public ICalFloatFormatter(int decimals) {
setMaximumFractionDigits(decimals);
if (decimals > 0) {
setMinimumFractionDigits(1);
}
//decimal separator differs by locale (e.g. Germany uses ",")
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setMinusSign('-');
setDecimalFormatSymbols(symbols);
}
/**
* Regardless of the default locale, comma ('.') is used as decimal separator
*
* @param source
* @return
* @throws ParseException
*/
public BigDecimal parse(String source) throws ParseException {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
DecimalFormat format = new DecimalFormat("#.#", symbols);
format.setParseBigDecimal(true);
return (BigDecimal) format.parse(source);
}
/**
* Constructor
*
* @param id
* @param description
* @param role
* @param value
* @param format
*/
public ScientificNotationExchangeItem (String id, String description, Role role, double value, String format) {
this.id = id;
this.description = description;
this.role = role;
this.value = value;
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
otherSymbols.setDecimalSeparator('.');
this.formatter = new DecimalFormat(format, otherSymbols);
}
private void fixThousandsSeparatorWithSpace(DecimalFormatSymbols symbols) {
if(Character.isSpaceChar(formatFormat.getDecimalFormatSymbols().getGroupingSeparator())){
symbols.setGroupingSeparator(' ');
}
if(Character.isWhitespace(formatFormat.getDecimalFormatSymbols().getDecimalSeparator())){
symbols.setDecimalSeparator(' ');
}
if(Character.isWhitespace(formatFormat.getDecimalFormatSymbols().getMonetaryDecimalSeparator())){
symbols.setMonetaryDecimalSeparator(' ');
}
}
@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());
}
}
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 static DecimalFormat newDF(String formatString) {
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
otherSymbols.setDecimalSeparator('.');
otherSymbols.setGroupingSeparator(' ');
//DecimalFormat df = new DecimalFormat("####.####",otherSymbols);
DecimalFormat df = new DecimalFormat("########.########", otherSymbols);
return df;
}
/**
* Procedure creates new dot-separated DecimalFormat
*/
public static DecimalFormat getDecimalFormat(String format)
{
DecimalFormat df = new DecimalFormat(format);
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
dfs.setExponentSeparator("e");
df.setDecimalFormatSymbols(dfs);
return df;
}
@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 FgLargeValuesFormatter() {
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault());
otherSymbols.setDecimalSeparator('.');
otherSymbols.setGroupingSeparator(Character.MIN_VALUE);
mFormat = new DecimalFormat("###E0", otherSymbols);
}
/** {@inheritDoc} */
@Override protected DecimalFormat initialValue() {
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setGroupingSeparator(',');
symbols.setDecimalSeparator('.');
String ptrn = "#,##0.0#";
DecimalFormat decimalFormat = new DecimalFormat(ptrn, symbols);
decimalFormat.setParseBigDecimal(true);
return decimalFormat;
}