Tag: 十进制格式

DecimalFormat精度无法正常工作

我想格式化使用DecimalFormat给定的精度的浮动。 我拥有的是这个 val formatter = DecimalFormat(if (precision > 0) "#0.${"0".repeat(precision)}" else "#") 可以说,精度是2,当我这样做 formatter.format(20.0f).toFloat() 我得到的输出为20.0f而不是20.00f

Android DecimalFormat格式不正确

我在TextWatcher使用DecimalFormat , TextWatcher是我的代码: 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) […]