字符串到小数点后两位

String to 2 decimal places

 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"

                   

寻求有关以下编码的帮助,以帮助我减少生成的结果以仅显示 2 位小数。这用于应用程序中,用户用户输入提供的值进行计算,并根据这些输入生成结果。

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

用户输入示例; Nn - 16 磅英尺 - 3.6

结果; Ngs = 0.576

如何将其限制为仅显示 2 位小数?某些结果显示多达 7 位小数,这对于应用目的而言是不需要的。

非常感谢任何帮助。

崩溃日志:

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$onItemSelected.
onClick(ImperialCalc.kt:152)
    at android.view.View.performClick(View.java:7217)
    at android.view.View.performClickInternal(View.java:7191)
    at android.view.View.access00(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

使用这个:

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())

请return回答小数点后两位。

使用String.format("%.2f", 你的值) 检查这个你的值是 float