making EditText to show only two decimal places

前端 未结 4 989
礼貌的吻别
礼貌的吻别 2021-01-12 12:28

i want to show only two decimal places in my edit text, ofc i wanna show currency in edit text but limiting its value to 2 digits after decimal.

I have seen some sol

4条回答
  •  天涯浪人
    2021-01-12 12:31

    Input:

     mBinding.etPriceUpdate.setText = getString(R.string.txt_rupees) + " " + "%.2f".format(firstNum!!.toDouble() * secondNum.toDouble())
    

    Output:

    ₹ 4567.54

    This will format the floating-point number 1.23456 up to 2 decimal places because we have used two after the decimal point in formatting instruction %.2f, f is for floating-point number, which includes both double and float data type in Java. Don't try to use "d" for double here, because that is used to format integer and stands for decimal in formatting instruction. By the way, there is a catch here, format() method will also arbitrarily round the number. For example, if you want to format 1.99999 up-to 2 decimal places then it will return 2.0 rather then 1.99, as shown below.

提交回复
热议问题