如何为计算器应用程序实时向数字添加数千分隔符.科特林



我有一个计算器应用程序,当用户实时输入他们的输入时,以及当计算器给出大于999的数字时,我希望它能显示大数字,数字之间有空格。目前,他们之间没有这样的空间:10000;100000;1000000。我想让他们这样表演:10000;100000;1000 000。第二个看起来整洁多了。

这是我的代码

 class CalculatorActivity : AppCompatActivity() {
@Suppress("PrivatePropertyName")
private var DigitOnScreen = StringBuilder(12)
private var operation: Char = ' '
private var leftHandSide: Double = 0.0
private var rightHandSide: Double = 0.0
private lateinit var binding: ActivityCalculatorBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityCalculatorBinding.inflate(layoutInflater)
    setContentView(binding.root)
    binding.resultId.text = "0"
    initializeButtons()
}
private fun initializeButtons() {
    functionalButtons()
    operationalButtons()
    numericalButtons()
}
/**
 * This function initializes all of our numerical buttons from
 *  [0 - 9]
 */
private fun numericalButtons() {
    binding.oneBtn.setOnClickListener {
        appendToDigitOnScreen("1")
    }
    binding.twoBtn.setOnClickListener {
        appendToDigitOnScreen("2")
    }
    binding.threeBtn.setOnClickListener {
        appendToDigitOnScreen("3")
    }
    binding.fourBtn.setOnClickListener {
        appendToDigitOnScreen("4")
    }
    binding.fiveBtn.setOnClickListener {
        appendToDigitOnScreen("5")
    }
    binding.sixBtn.setOnClickListener {
        appendToDigitOnScreen("6")
    }
    binding.sevenBtn.setOnClickListener {
        appendToDigitOnScreen("7")
    }
    binding.eightBtn.setOnClickListener {
        appendToDigitOnScreen("8")
    }
    binding.nineBtn.setOnClickListener {
        appendToDigitOnScreen("9")
    }
    binding.zeroBtn.setOnClickListener {
        appendToDigitOnScreen("0")
    }
    binding.dotBtn.setOnClickListener {
        appendToDigitOnScreen(".")
    }
}
/**
 *  Insert the button been clicked onto the screen so user can see
 *  inputs for the button clicked
 */
private fun appendToDigitOnScreen(digit: String) {
    // Add each digit to our string builder
    DigitOnScreen.append(digit)
    // display it on the screen of our mobile app
    binding.resultId.text = DigitOnScreen.toString()
}
/**
 *  Initialize the operation keys in our calculator like the
 *  addition key, subtraction key and the likes
 */
private fun operationalButtons() {
    binding.additionBtn.setOnClickListener {
        selectOperation('A')
    }
    binding.subtractBtn.setOnClickListener {
        selectOperation('S')
    }
    binding.divideBtn.setOnClickListener {
        selectOperation('D')
    }
    binding.multipyBtn.setOnClickListener {
        selectOperation('M')
    }
}
/**
 * Function to assign operational sign to our math calculations
 */
private fun selectOperation(c: Char) {
    operation = c
    leftHandSide = DigitOnScreen.toString().toDouble()
    DigitOnScreen.clear()
    binding.resultId.text = "0"
}
/**
 * Handles functional operations in out application like
 * clear button, backspace button and the clear everything button
 */
private fun functionalButtons() {
    binding.clearEverythingBtn.setOnClickListener {
        DigitOnScreen.clear()
        binding.resultId.text = "0"
    }
    binding.clearBtn.setOnClickListener {
        if (DigitOnScreen.isEmpty()) {
            return@setOnClickListener
        } else {
            clearDigit()
        }
    }
    binding.backspaceBtn.setOnClickListener {
        if (DigitOnScreen.isEmpty()) {
            return@setOnClickListener
        } else {
            clearDigit()
        }
    }
    binding.equalBtn.setOnClickListener {
        performMathOperation()
    }
}

/**
 *  This function performs our Math Operation which is then showed on the screen.
 */
private fun performMathOperation() {
    rightHandSide = DigitOnScreen.toString().toDouble()

    when (operation) {
        'A' -> {
            val sum = OperationsHelper.add(leftHandSide, rightHandSide)
            binding.resultId.text = sum.toString()
            DigitOnScreen.clear()
            DigitOnScreen.append(sum)
        }
        'S' -> {
            val subtract = OperationsHelper.subtract(leftHandSide, rightHandSide)
            binding.resultId.text = subtract.toString()
            DigitOnScreen.clear()
            DigitOnScreen.append(subtract)
        }
        'M' -> {
            val multiply = OperationsHelper.multiply(leftHandSide, rightHandSide)
            binding.resultId.text = multiply.toString()
            DigitOnScreen.clear()
            DigitOnScreen.append(multiply)
        }
        'D' -> {
            val divide = OperationsHelper.divide(leftHandSide, rightHandSide)
            binding.resultId.text = divide.toString()
            DigitOnScreen.clear()
            DigitOnScreen.append(divide)
        }
    }
}
/**
 *  This function remove the last digit on the screen.
 */
private fun clearDigit() {
    val length = DigitOnScreen.length
    DigitOnScreen.deleteCharAt(length - 1)
    if (length <= 0) {
        binding.resultId.text = "0"
    }else{
        binding.resultId.text = DigitOnScreen.toString()
    }
}

我需要一个专门针对文本的答案查看而非编辑文本

您可以使用格式说明符,包含特定于区域设置的组分隔符。我会首先尝试这样写:

binding.resultId.text = String.format("%,f", DigitOnScreen.toString().toDouble())

然后,要获得正确的小数位数,,即对应于用户输入的,您必须对小数分隔符后的数字进行计数。这里明确使用.作为DigitOnScreen的十进制分隔符。

val numberAsString = DigitOnScreen.toString()
val nofDecimals = numberAsString.substringAfter('.', "").length
binding.resultId.text = String.format("%,.${nofDecimals}f", numberAsString.toDouble())

还要注意,在您的情况下,DigitOnScreen不是屏幕上显示的数字,在屏幕上(binding.resultId.text中的内容(,组和小数分隔符将取决于当前应用程序的区域设置。

相关内容

  • 没有找到相关文章