【问题标题】:Changing the decimal separator更改小数点分隔符
【发布时间】:2017-10-28 10:48:23
【问题描述】:

我想更改应用中数字的分隔符。我用十进制格式试了一下。

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

但我得到了例外,因为 十进制格式不支持符号 (')。我什至不知道我做的是对还是错。我只需要像12'23'52 这样的格式。还有一项要求是,小数点分隔符(.)应替换为(,)。

谁能帮帮我。谢谢你。 :)

【问题讨论】:

  • 检查我下面的答案是否正常
  • 你可以查看我的答案。
  • 如果答案有效,则标记为已接受

标签: java android


【解决方案1】:

试试这个

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));

【讨论】:

    【解决方案2】:

    我将这个类用于 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));
    

    【讨论】:

      【解决方案3】:

      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
      

      【讨论】:

      • 是的,即使我尝试了您的代码,它也可以正常工作,但这不是正确的做法。如果您想在每次需要将所有, 替换为' 时格式化100 或1000 个值。
      • 是的,你是对的。但这是我能想到的一种方式。你的回答是正确的。谢谢你的解释。@SushinPv。我支持你。
      • 谢谢,它有效。但每当我想使用数字时,我需要将所有分隔符替换为“”。
      • @jothi 是的,你必须这样做。检查以下答案
      猜你喜欢
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      相关资源
      最近更新 更多