【问题标题】:Android DecimalFormat doesn't format correctlyAndroid DecimalFormat 格式不正确
【发布时间】:2017-09-07 06:18:17
【问题描述】:

我在TextWatcher 中使用DecimalFormat,这是我的代码:

override fun afterTextChanged(p0: Editable?) {
            amountEt?.removeTextChangedListener(this)

            var df = DecimalFormat("#,###.##")
            df.maximumFractionDigits = 2
            df.minimumFractionDigits = 0
            if (hasFractionalPart) {
                df.isDecimalSeparatorAlwaysShown = true
            }
            try {
                val inilen: Int
                val endlen: Int
                inilen = amountEt?.text!!.length

                var text: String? = p0.toString().replace(df.decimalFormatSymbols.groupingSeparator.toString(), "")
                //text = text?.replace(df.decimalFormatSymbols.decimalSeparator.toString(), "")
                var n = df.parse(text)
                var t = df.format(n)
                val cp = amountEt?.selectionStart
                amountEt?.setText(t)
                endlen = amountEt?.text!!.length
                val sel = cp!!.plus(endlen - inilen)
                if (sel > 0 && sel <= amountEt?.text!!.length) {
                    amountEt?.setSelection(sel)
                } else {
                    // place cursor at the end?
                    amountEt?.setSelection(amountEt?.text!!.length - 1)
                }
            } catch (e: Exception) {
                Log.d("ERROR", e.stackTrace.toString())
            }

            amountEt?.addTextChangedListener(this)
        }

我的问题是用户想写入amountEt(这是一个EditText),例如:1.02

但是当用户将 zero 写入我的编辑文本时,df.format(n) 行结果将是 1

我该如何解决这个问题?

更新: 我调试了我的代码,如果我将 7.0 写入编辑文本,我得到了这个:

text = "7.0"

n = 7(类型为数字)

如果我改变这一行:

var n = df.parse(text)

到这里:

var n = df.parse(text).toDouble()

n = 7.0

t = "7."

这是关于我的调试器的图片:

【问题讨论】:

  • 您可以尝试以下操作吗:with(DecimalFormat.getInstance(Locale.US)) { maximumFractionDigits = 2; minimumFractionDigits = 0; val n = parse("7.12"); println(n); println(format(n)); } 它为我打印“7.12, 7.12”。所以唯一的区别是DecimalFormat 是如何创建的。我不确定你是否真的需要这种模式?
  • 我试过你的代码,它写了 7.12,我改变了你的代码以使用我的文本变量,但是当我输入编辑文本“7”时它只写了 7。
  • 设置minimumFractionDigits = 1

标签: java android kotlin decimalformat


【解决方案1】:

尝试使用 DecimalFormat("#,###.00") 而不是 DecimalFormat("#,###.##")

【讨论】:

    【解决方案2】:

    您可以使用此代码转换为双精度

    public static double Round(double value, int places) {
            if (places < 0) throw new IllegalArgumentException();
            long factor = (long) Math.pow(10, places);
            value = value * factor;
            long tmp = Math.round(value);
            return (double) tmp / factor;
        }
    

    【讨论】:

    • 我转换成Double没问题,问题是DecimalFormat格式方法把0(零)截掉了
    • 我试过你的方法,这就像 toDouble() 方法。
    【解决方案3】:

    您好,请检查下面的十进制格式代码

    public static String roundTwoDecimalsString(double d) {
            /*DecimalFormat twoDForm = new DecimalFormat("#.##");*/
            DecimalFormat df = new DecimalFormat("#.##");
            String formatted = df.format(d);
            return formatted;
        }
    

    【讨论】:

    • 没关系,但我也想使用分组分隔符。使用您的解决方案,它不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多