0
 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

val arrayAdapter = activity?.let { 
ArrayAdapter(it,android.R.layout.simple_spinner_dropdown_item, 
resources.getStringArray(R.array.nutrients)) }


    spinner.adapter = arrayAdapter
    spinner.onItemSelectedListener = object : 
AdapterView.OnItemSelectedListener {

        override fun onNothingSelected(p0: AdapterView<*>?) {
        }

        override  fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, 
p3: Long) {
            val nutrient = spinner.selectedItem.toString()
            if (nutrient == "N") {
                Npounds.text = "N Lbs /1000 sq ft"
                Nn.error = null
                Pp.error = null
                Kk.error = null
                NLbs.error = null
                lbsft.error = null

            }
            if (nutrient == "P") {
                Npounds.text = "P Lbs /1000 sq ft"
                Nn.error = null
                Pp.error = null
                Kk.error = null
                NLbs.error = null
                lbsft.error = null
            }
            if (nutrient == "K") {
                Npounds.text = "K Lbs /1000 sq ft"
                Nn.error = null
                Pp.error = null
                Kk.error = null
                NLbs.error = null
                lbsft.error = null
            }


Calculates.setOnClickListener {




if (nutrient == "N") {
Pp.error = null
Kk.error = null
if (TextUtils.isEmpty(Nn.text.toString())) {
Nn.error =
"Please insert N% value and either N Lbs /1000 sq ft OR Lbs / 1000 sq ft!"
return@setOnClickListener

} else if (TextUtils.isEmpty(NLbs.text.toString()) && TextUtils.isEmpty(
lbsft.text.toString())) {
NLbs.error ="Please insert N% value and either N Lbs /1000 sq ft OR Lbs / 
1000 sq ft!"
lbsft.error ="Please insert N% value and either N Lbs /1000 sq ft OR Lbs / 
1000 sq ft!"
return@setOnClickListener

} else {
Nn.error = null
NLbs.error = null
lbsft.error = null
}
if (NLbs.text.toString().isNotBlank() && lbsft.text.toString().isNotBlank() {
NLbs.error ="Please insert for either N Lbs /1000 sq ft OR Lbs / 1000 sq ft!"
lbsft.error ="Please insert for either N Lbs /1000 sq ft OR Lbs / 1000 sq 
ft!"

} else {
Nn.error = null
NLbs.error = null
lbsft.error = null
}


val nitrograms = if (NLbs.text.toString().isNotBlank()){ 
NLbs.text.toString()
} else {
(Nn.text.toString().toFloat() / 100 * lbsft.text.toString()
.toFloat() ).toString()
}

Ngs.text = "$nitrograms Lbs of N / 1000 sq ft"

                   

Looking for some assistance with the following coding to help me to reduce the produced result to display only 2 decimal places. This is used in an app which user user inputs to provided values for calculation and produces a result based on those inputs.

 val nitrograms = (Nn.text.toString().toFloat() / 100 * lbsft.text.toString().toFloat()).toString()
  
  Ngs.text = "$nitrograms Lbs of N / 1000 sq ft"

example user inputs; Nn - 16 lbsft - 3.6

result; Ngs = 0.576

How do i limit this to only display 2 decimal places? Some results display as many as 7 decimal places which aren't needed for the apps purpose.

Any assistance much appreciated.

Crash log:

2020-09-04 15:42:20.878 2188-2188/com.lawncareecosse.fertcalcpro 
I/BlockMonitor: dispatchingThrewException In MainThread
2020-09-04 15:42:20.878 2188-2188/com.lawncareecosse.fertcalcpro 
D/AndroidRuntime: Shutting down VM
2020-09-04 15:42:20.878 2188-2188/com.lawncareecosse.fertcalcpro I/QarthLog: 
[PatchStore] createDisableExceptionQarthFile
2020-09-04 15:42:20.879 2188-2188/com.lawncareecosse.fertcalcpro I/QarthLog: 
[PatchStore] create disable file for com.lawncareecosse.fertcalcpro uid is 
10330
2020-09-04 15:42:20.880 2188-2188/com.lawncareecosse.fertcalcpro 
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.lawncareecosse.fertcalcpro, PID: 2188
java.util.IllegalFormatConversionException: f != java.lang.String
    at 
java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4403)
    at java.util.Formatter$FormatSpecifier.printFloat(Formatter.java:2898)
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2845)
    at java.util.Formatter.format(Formatter.java:2524)
    at java.util.Formatter.format(Formatter.java:2459)
    at java.lang.String.format(String.java:2870)
    at 
com.lawncareecosse.fertcalcpro.ImperialCalc$onViewCreated$1$onItemSelected$1.
onClick(ImperialCalc.kt:152)
    at android.view.View.performClick(View.java:7217)
    at android.view.View.performClickInternal(View.java:7191)
    at android.view.View.access$3500(View.java:828)
    at android.view.View$PerformClick.run(View.java:27679)
    at android.os.Handler.handleCallback(Handler.java:900)
    at android.os.Handler.dispatchMessage(Handler.java:103)
    at android.os.Looper.loop(Looper.java:219)
    at android.app.ActivityThread.main(ActivityThread.java:8347)
    at java.lang.reflect.Method.invoke(Native Method)
    at 
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:
513)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
2020-09-04 15:42:20.890 2188-2188/com.lawncareecosse.fertcalcpro I/Process: 
Sending signal. PID: 2188 SIG: 9

2 Answers2

1

Use this:

val nitrograms = (Nn.text.toString().toFloat() / 100 * lbsft.text.toString().toFloat()).toString()

// java.util.IllegalFormatConversionException: f != java.lang.String is thrown because you are passing a String to the format function instead of a Float.

// Calling toFloat() on the nitrograms will remove the error.
  
Ngs.text = "%.2f Lbs of N / 1000 sq ft".format(nitrograms.toFloat())

Will return answer in two decimal places.

Andrew Chelix
  • 1,012
  • 11
  • 16
0

Use String.format("%.2f", your value) Check this your value is float