2017-10-28 60 views
3

我想更改我的應用中數字的分隔符。我用十進制格式試了一下。更改小數點分隔符

DecimalFormat x=new DecimalFormat("##'##'##.###"); 
textview.setText(x.format("1564643"); 

但我得到的例外,因爲符號(')不DecimalFormat的支持。我甚至不知道我所做的是對還是不對。我只需要一個像12'23'52這樣的格式。還有一個要求是,小數位分隔符(。)應該替換爲(,)。

任何人都可以幫助我。謝謝。 :)

+0

檢查我下面的回答能夠正常工作 –

+0

您可以檢查我的答案。 – KeLiuyue

+0

如果答案正在接受工作標記 –

回答

0

1.使用new DecimalFormat("##,##");格式化

2.使用replaceAll(",", "'");更換

3.使用setText設置文本到TextView

試試這個。

DecimalFormat mFormat = new DecimalFormat("##,##"); 
String yourFormattedString = mFormat.format(1564643); 
String result = yourFormattedString.replaceAll(",", "'"); 
Log.e("DecimalFormat", result); 
textview.setText(result); 

輸出

1'56'46'43 
+0

這不是正確的做法 –

+0

我在我的代碼中嘗試它,並且它運行良好。 – KeLiuyue

+0

是的,即使我嘗試了你的代碼,但它並不是正確的做法。如果你想在每次需要用'''替換所有','時格式化爲100或1000的值。 –

0

我使用這個類的EditText。你可以使用其中的一部分來解決你的問題。

public class NumberTextWatcherWithSeperator implements TextWatcher { 

private EditText editText; 

public NumberTextWatcherWithSeperator(EditText editText) { 
    this.editText = editText; 
} 

@Override 
public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

} 

@Override 
public void onTextChanged(CharSequence s, int start, int before, int count) { 

} 

@Override 
public void afterTextChanged(Editable s) { 
    try { 
     editText.removeTextChangedListener(this); 
     String value = editText.getText().toString(); 

     if (!value.equals("")) { 

      if (value.startsWith(".")) { 
       editText.setText("0."); 
      } 
      if (value.startsWith("0") && !value.startsWith("0.")) { 
       editText.setText(""); 

      } 

      String str = editText.getText().toString().replaceAll("'", ""); 
      if (!value.equals("")) 
       editText.setText(getDecimalFormattedString(str)); 
      editText.setSelection(editText.getText().toString().length()); 
     } 
     editText.addTextChangedListener(this); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     editText.addTextChangedListener(this); 
    } 
} 

private static String getDecimalFormattedString(String value) { 
    StringTokenizer lst = new StringTokenizer(value, "."); 
    String str1 = value; 
    String str2 = ""; 
    if (lst.countTokens() > 1) { 
     str1 = lst.nextToken(); 
     str2 = lst.nextToken(); 
    } 
    StringBuilder str3 = new StringBuilder(); 
    int i = 0; 
    int j = -1 + str1.length(); 
    if (str1.charAt(-1 + str1.length()) == '.') { 
     j--; 
     str3 = new StringBuilder("."); 
    } 
    for (int k = j; ; k--) { 
     if (k < 0) { 
      if (str2.length() > 0) 
       str3.append(".").append(str2); 
      return str3.toString(); 
     } 
     if (i == 2) { 
      str3.insert(0, "'"); 
      i = 0; 
     } 
     str3.insert(0, str1.charAt(k)); 
     i++; 
    } 

} 
} 

,並在活動中使用這樣的:

EditText editText = findViewById(R.id.et); 
    editText.addTextChangedListener(new NumberTextWatcherWithSeperator(editText)); 
4

試試這個

Locale currentLocale = Locale.getDefault(); 
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(currentLocale); 
otherSymbols.setGroupingSeparator('\''); 

DecimalFormat formatter = new DecimalFormat("#,#0",otherSymbols); 
//If you want to parse the String use 
//formatter.format(Integer.parseInt("1564643")) 
textview.setText(formatter.format(1564643));