0

I'm making a program to calculate taxes, and I want to make the numbers typed in editText be separated in real time with decimal places (commas). Separate in decimal places both the numbers to be typed in the editText and the result that will be obtained after the calculations. Thanks!!!

Here is the code that does the respective calculations. KOTLIN...

package com.marianomuendane.contabiliadadeVocacional

import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.*

class IRPCFragment : Fragment() {

//variables for the respective calculations

    private lateinit var proveitos: EditText
    private lateinit var custos: EditText
    private lateinit var amortizacoes: EditText
    private lateinit var juros: EditText

    private lateinit var format: String
    private lateinit var decimal: DecimalFormat
    private lateinit var decimalSymbols: DecimalFormatSymbols

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view = inflater.inflate(R.layout.fragment_i_r_p_c, container, false)

        proveitos = view.findViewById(R.id.proveitos)

        proveitos.setRawInputType(Configuration.KEYBOARD_12KEY)

//decimal number attempt
        //proveitos.addTextChangedListener(editText1())

        decimalSymbols = DecimalFormatSymbols(Locale("pt", "Brazil"))
        decimalSymbols.setDecimalSeparator(',')
        decimalSymbols.setGroupingSeparator('.')

        format = "###,###,###,###.####"

        decimal = DecimalFormat(format)

        // EditText necessarios

        //==========================================================

        custos = view.findViewById<EditText>(R.id.custos)

        custos.setRawInputType(Configuration.KEYBOARD_12KEY)

        //==========================================================

        amortizacoes = view.findViewById<EditText>(R.id.amortizacoes)

        amortizacoes.setRawInputType(Configuration.KEYBOARD_12KEY)

        //==========================================================

        juros = view.findViewById(R.id.juros)

        juros.setRawInputType(Configuration.KEYBOARD_12KEY)

        //==========================================================

        val imposto = view.findViewById<TextView>(R.id.imposto)
        imposto.setTextIsSelectable(true)

        val bt4 = view.findViewById<Button>(R.id.res_final)
        val resolucao = view.findViewById<Button>(R.id.resolucao_Irpc)

        //======================================================================================

//function of calculations
        fun iRPC() =
            if (proveitos.text.isEmpty() && custos.text.isEmpty() && amortizacoes.text.isEmpty()
                && juros.text.isEmpty()
            ) {
                Toast.makeText(requireContext(), "Preencha os valores ", Toast.LENGTH_SHORT).show()

            } else if (proveitos.text.isEmpty()) {
                Toast.makeText(requireContext(), "Preencha proveitos", Toast.LENGTH_SHORT).show()

            } else if (custos.text.isEmpty()) {
                Toast.makeText(requireContext(), "Preencha custos", Toast.LENGTH_SHORT).show()

            } else if (amortizacoes.text.isEmpty()) {
                Toast.makeText(requireContext(), "Preencha amortizacoes", Toast.LENGTH_SHORT).show()

            } else if (juros.text.isEmpty()) {
                Toast.makeText(requireContext(), "Preencha juros", Toast.LENGTH_SHORT).show()

            } else {

                val prov = proveitos.text.toString().toFloat()
                val cust = custos.text.toString().toFloat()
                val amort = amortizacoes.text.toString().toFloat()
                val jur = juros.text.toString().toFloat()

                val result = (prov - cust - amort - jur) * 0.32
                val formatResult = decimal.format(result)

                imposto.setText(result.toString())
            }

        //======================================================================================

        bt4.setOnClickListener {
            iRPC()
        }

        return view
    }

}

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jan 04 '23 at 20:08
  • Please remember that Stack Overflow is not your favourite Kotlin forum, but rather a question and answer site for all programming related questions. Thus, please always include the tag of the language you are programming in, that way other users familiar with that language can more easily find your question. Take the [tour] and read up on [ask] to get more information on how this site works, then [edit] the question with the relevant tags. – Adriaan Jan 09 '23 at 10:51
  • I want to make the numbers typed in Editext to make a calculation to be separated by decimal places (comma) in real time. And the result is also separated into decimal places. Like a basic calculator but with dynamic separation of numbers by decimal places. – Mariano Muendane Jan 09 '23 at 10:52
  • Is this what you want? (https://stackoverflow.com/a/45299775/13785815) The code uses `TextWatcher` to watch changes and modify the text accordingly. As for the calculation, you can get texts from `EditText`s and convert them to `BigDecimal`s. Once the calculation is done, you can format the result and put it in your result area. – Simon Smith Jan 09 '23 at 11:53

0 Answers0