如何强制小数点后两位数

2024-04-07

我有以下 TextView,它始终显示 $0.00,除非在单击按钮并保存到此 TextView 后完成任何计算。

        <TextView
            android:id="@+id/tvTotalAmount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="$0.00"
            android:textStyle="bold"
            android:paddingTop="10dp"
            android:textColor="#00A21E"
            android:textSize="25dp"
            android:paddingLeft="30dp"
            android:layout_below="@id/tvTotal" />
            <EditText
                android:id="@+id/etToll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="numberDecimal" />
            <Button
                android:id="@+id/btnCalculate"
                android:background="@drawable/calcbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Calculate"
                android:layout_gravity="right"
                android:textStyle="bold"
                android:textColor="#FFFFFF"
                android:shadowColor="#000000"
                android:shadowDx="1"
                android:shadowDy="1"
                android:shadowRadius="2"
                android:layout_alignParentRight="true" />

当我运行该应用程序时,它显示 $0.0(这可以工作,但我正在使用美元和美分,并且我希望它显示小数点后两位数,除非两位数都是 00)

我在用SharedPreferences这是我在应用程序中的代码:

onCreate()

float totalTollAmount = 0;
    tollAmount = (EditText) mFrame2.findViewById(R.id.etToll); //where the amount is entered
    tollAmount.setFilters(new InputFilter[] {new DecimalFilter(6, 2)});
    tvTotalAmountLabel = (TextView) mFrame2.findViewById(R.id.tvTotalAmount); //holding total amount
    if (Float.toString(prefs.getFloat("dblTollAmount", 0)) != "0.0") {
        tollAmount.setText(Float.toString(prefs.getFloat("dblTollAmount", 0)));
    }
    tvTotalAmountLabel.setText("$" + Float.toString(prefs.getFloat("totalTollAmount", 0))); //retrieves the total toll amount and displays

btnCalc.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        strTollAmount = tollAmount.getText().toString();
        strInfName = nameOfInf.getText().toString();
        String k = "";
        if (strTollAmount.length() != 0 && strInfName.length() != 0) {
            float dblTollAmount = Float.parseFloat(strTollAmount);
            submitInfo(dblTollAmount);
        }
        Toast.makeText(getActivity(), k, Toast.LENGTH_SHORT).show();
    }
});

    private void submitInfo(float toll) {
        y = "TEST";
        if (y != null) {
            editor.putString("someId", y);
            totalTollAmount = round(toll + prefs.getFloat("totalTollAmount", 0));
            tvTotalAmountLabel.setText("$" + Float.toString(totalTollAmount));
            }
              else {
            editor.putString("someId", "");
        }
        editor.putString("previous-trip", y);
        editor.putFloat("totalTollAmount", round(totalTollAmount));
        //editor.commit(); removed on 8/3
        if (nameOfInf != null && nameOfInf.length() > 0) { // added on 8/3...
            //Clear out this value from Enter Data tab ONLY if Calculate happens
        }
        if (tollAmount != null && tollAmount.length() > 0) {
            //Clear out this value from the Enter Data tab ONLY if Calculate happens
        }
        editor.commit(); //added on 8/3
    }

private float round(float amount){
    return Float.parseFloat(new DecimalFormat("###.##").format(amount));
}

onDestroy method:

@Override
public void onDestroy(){
    super.onDestroy();
    Log.i("OnDestroy", "Logged");
    if(nameOfInf != null && nameOfInf.length() > 0) {
        editor.putString("strInfName", nameOfInf.getText().toString());
    }
    if(tollAmount != null && tollAmount.length() > 0) {
        editor.putFloat("dblTollAmount", Float.parseFloat(tollAmount.getText().toString()));
    }

    editor.commit();
}

它应该做什么: 每次我击中calculate按钮应用程序应采用 EditText 上的内容etToll并添加到 TextView 上已有的值tvTotalAmount并且每次都会继续这样做。

尽管 TextView 的原始值为 $0.00,但它只显示 $0.0。我想在 EditText 和 TextView 上显示小数点后两位数字,因为我将使用美元和美分。

我该如何解决我面临的问题?

如果需要完整代码,请告诉我。


在您的代码中使用它

DecimalFormat form = new DecimalFormat("0.00");
String FormattedText=form.format(your value);

将其设置为 Edittext..它将起作用

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何强制小数点后两位数 的相关文章

随机推荐