下面列出了java.text.DecimalFormatSymbols#setGroupingSeparator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
private void printTestInfo(int maxCacheSize) {
DecimalFormat grouped = new DecimalFormat("000,000");
DecimalFormatSymbols formatSymbols = grouped.getDecimalFormatSymbols();
formatSymbols.setGroupingSeparator(' ');
grouped.setDecimalFormatSymbols(formatSymbols);
System.out.format(
"Test will use %s bytes of memory of %s available%n"
+ "Available memory is %s with %d bytes pointer size - can save %s pointers%n"
+ "Max cache size: 2^%d = %s elements%n",
grouped.format(ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
grouped.format(Runtime.getRuntime().maxMemory()),
grouped.format(Runtime.getRuntime().maxMemory()
- ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
Unsafe.ADDRESS_SIZE,
grouped.format((Runtime.getRuntime().freeMemory()
- ShrinkAuxiliaryDataTest.getMemoryUsedByTest())
/ Unsafe.ADDRESS_SIZE),
maxCacheSize,
grouped.format((int) Math.pow(2, maxCacheSize))
);
}
private void printTestInfo(int maxCacheSize) {
DecimalFormat grouped = new DecimalFormat("000,000");
DecimalFormatSymbols formatSymbols = grouped.getDecimalFormatSymbols();
formatSymbols.setGroupingSeparator(' ');
grouped.setDecimalFormatSymbols(formatSymbols);
System.out.format(
"Test will use %s bytes of memory of %s available%n"
+ "Available memory is %s with %d bytes pointer size - can save %s pointers%n"
+ "Max cache size: 2^%d = %s elements%n",
grouped.format(ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
grouped.format(Runtime.getRuntime().maxMemory()),
grouped.format(Runtime.getRuntime().maxMemory()
- ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
Unsafe.ADDRESS_SIZE,
grouped.format((Runtime.getRuntime().freeMemory()
- ShrinkAuxiliaryDataTest.getMemoryUsedByTest())
/ Unsafe.ADDRESS_SIZE),
maxCacheSize,
grouped.format((int) Math.pow(2, maxCacheSize))
);
}
private void printTestInfo(int maxCacheSize) {
DecimalFormat grouped = new DecimalFormat("000,000");
DecimalFormatSymbols formatSymbols = grouped.getDecimalFormatSymbols();
formatSymbols.setGroupingSeparator(' ');
grouped.setDecimalFormatSymbols(formatSymbols);
System.out.format(
"Test will use %s bytes of memory of %s available%n"
+ "Available memory is %s with %d bytes pointer size - can save %s pointers%n"
+ "Max cache size: 2^%d = %s elements%n",
grouped.format(ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
grouped.format(Runtime.getRuntime().maxMemory()),
grouped.format(Runtime.getRuntime().maxMemory()
- ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
Unsafe.ADDRESS_SIZE,
grouped.format((Runtime.getRuntime().freeMemory()
- ShrinkAuxiliaryDataTest.getMemoryUsedByTest())
/ Unsafe.ADDRESS_SIZE),
maxCacheSize,
grouped.format((int) Math.pow(2, maxCacheSize))
);
}
private void printTestInfo(int maxCacheSize) {
DecimalFormat grouped = new DecimalFormat("000,000");
DecimalFormatSymbols formatSymbols = grouped.getDecimalFormatSymbols();
formatSymbols.setGroupingSeparator(' ');
grouped.setDecimalFormatSymbols(formatSymbols);
System.out.format(
"Test will use %s bytes of memory of %s available%n"
+ "Available memory is %s with %d bytes pointer size - can save %s pointers%n"
+ "Max cache size: 2^%d = %s elements%n",
grouped.format(ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
grouped.format(Runtime.getRuntime().maxMemory()),
grouped.format(Runtime.getRuntime().maxMemory()
- ShrinkAuxiliaryDataTest.getMemoryUsedByTest()),
Unsafe.ADDRESS_SIZE,
grouped.format((Runtime.getRuntime().freeMemory()
- ShrinkAuxiliaryDataTest.getMemoryUsedByTest())
/ Unsafe.ADDRESS_SIZE),
maxCacheSize,
grouped.format((int) Math.pow(2, maxCacheSize))
);
}
@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 DecimalFormat buildFormatWithSpaceGroupingSeparator() {
DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.ENGLISH);
DecimalFormatSymbols symbols = decimalFormat.getDecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
decimalFormat.setDecimalFormatSymbols(symbols);
return decimalFormat;
}
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(' ');
}
}
public static String transformToText(Transform t){
DecimalFormat dFormat4 = new DecimalFormat("0.0000");
DecimalFormat dFormat1 = new DecimalFormat("0.0");
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols();
otherSymbols.setDecimalSeparator('.');
otherSymbols.setGroupingSeparator(',');
dFormat4.setDecimalFormatSymbols(otherSymbols);
dFormat1.setDecimalFormatSymbols(otherSymbols);
StringBuilder sb = new StringBuilder();
Vector3f trans = t.getTranslation();
sb.append( dFormat4.format(trans.x) ).append(" ");
sb.append( dFormat4.format(trans.y) ).append(" ");
sb.append( dFormat4.format(trans.z) ).append(" ");
final Vector3f angles = t.getRotation().toAngles(null).multLocal( M.RAD_TO_DEG );
sb.append(dFormat1.format( angles.x ) ).append(" ");
sb.append(dFormat1.format( angles.y ) ).append(" ");
sb.append(dFormat1.format( angles.z ) ).append(" ");
Vector3f scale = t.getScale();
sb.append( dFormat4.format(scale.x) ).append(" ");
sb.append( dFormat4.format(scale.y) ).append(" ");
sb.append( dFormat4.format(scale.z) );
return sb.toString();
}
public Value num2str( String format, String decimalSymbol, String groupingSymbol, String currencySymbol ) throws KettleValueException {
if ( isNull() ) {
setType( VALUE_TYPE_STRING );
} else {
// Number to String conversion...
if ( getType() == VALUE_TYPE_NUMBER || getType() == VALUE_TYPE_INTEGER ) {
NumberFormat nf = NumberFormat.getInstance();
DecimalFormat df = (DecimalFormat) nf;
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
if ( currencySymbol != null && currencySymbol.length() > 0 ) {
dfs.setCurrencySymbol( currencySymbol );
}
if ( groupingSymbol != null && groupingSymbol.length() > 0 ) {
dfs.setGroupingSeparator( groupingSymbol.charAt( 0 ) );
}
if ( decimalSymbol != null && decimalSymbol.length() > 0 ) {
dfs.setDecimalSeparator( decimalSymbol.charAt( 0 ) );
}
df.setDecimalFormatSymbols( dfs ); // in case of 4, 3 or 2
if ( format != null && format.length() > 0 ) {
df.applyPattern( format );
}
try {
setValue( nf.format( getNumber() ) );
} catch ( Exception e ) {
setType( VALUE_TYPE_STRING );
setNull();
throw new KettleValueException( "Couldn't convert Number to String " + e.toString() );
}
} else {
throw new KettleValueException( "Function NUM2STR only works on Numbers and Integers" );
}
}
return this;
}
public NormalValuesFormatter() {
mFormat = NumberFormat.getCurrencyInstance();
mFormat.setMinimumFractionDigits(0);
mFormat.setMaximumFractionDigits(2);
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol("");
dfs.setGroupingSeparator(' ');
dfs.setMonetaryDecimalSeparator('.');
((DecimalFormat) mFormat).setDecimalFormatSymbols(dfs);
}
public synchronized String format(BigDecimal amount){
if (mCabbage != null) {
mNumberFormat.setMinimumFractionDigits(mCabbage.getDecimalCount());
mNumberFormat.setMaximumFractionDigits(mCabbage.getDecimalCount());
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setCurrencySymbol(mCabbage.getSimbol());
dfs.setGroupingSeparator(' ');
dfs.setMonetaryDecimalSeparator('.');
((DecimalFormat) mNumberFormat).setDecimalFormatSymbols(dfs);
return mNumberFormat.format(amount.setScale(mCabbage.getDecimalCount(), mRoundingMode));
} else {
return "";
}
}
/**
* Returns a thousans separated decimar formatter.
*
* @return
*/
public static DecimalFormat getThousandSeperatedDecimalFormat() {
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.US);
DecimalFormatSymbols symbols = df.getDecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
df.setDecimalFormatSymbols(symbols);
return df;
}
@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 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());
}
}
@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(Common.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 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;
}
@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());
}
}
/**
* {@inheritDoc}
*/
@Override
public DecimalFormatSymbols getInstance(final Locale locale) throws IllegalArgumentException, NullPointerException {
if (locale == null) {
throw new NullPointerException("locale:null");
} else if (!SakaiLocaleServiceProviderUtil.isAvailableLocale(locale)) {
throw new IllegalArgumentException("locale:" + locale.toString());
}
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator(
SakaiLocaleServiceProviderUtil.getChar("DecimalSeparator", locale));
symbols.setDigit(
SakaiLocaleServiceProviderUtil.getChar("Digit", locale));
symbols.setExponentSeparator(
SakaiLocaleServiceProviderUtil.getString("ExponentSeparator", locale));
symbols.setGroupingSeparator(
SakaiLocaleServiceProviderUtil.getChar("GroupingSeparator", locale));
symbols.setInfinity(
SakaiLocaleServiceProviderUtil.getString("Infinity", locale));
symbols.setInternationalCurrencySymbol(
SakaiLocaleServiceProviderUtil.getString("InternationalCurrencySymbol", locale));
symbols.setCurrencySymbol(
SakaiLocaleServiceProviderUtil.getString("CurrencySymbol", locale));
symbols.setMinusSign(
SakaiLocaleServiceProviderUtil.getChar("MinusSign", locale));
symbols.setMonetaryDecimalSeparator(
SakaiLocaleServiceProviderUtil.getChar("MonetaryDecimalSeparator", locale));
symbols.setNaN(
SakaiLocaleServiceProviderUtil.getString("NaN", locale));
symbols.setPatternSeparator(
SakaiLocaleServiceProviderUtil.getChar("PatternSeparator", locale));
symbols.setPercent(
SakaiLocaleServiceProviderUtil.getChar("Percent", locale));
symbols.setPerMill(
SakaiLocaleServiceProviderUtil.getChar("PerMill", locale));
symbols.setZeroDigit(
SakaiLocaleServiceProviderUtil.getChar("ZeroDigit", locale));
return symbols;
}
/** {@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;
}