如何强制两位数小数点后小数点、两位数

2023-09-04 07:25:21 作者:孤身歸故地

我有以下的TextView它显示$ 0.00所有的时间,除非任何计算后,点击一个按钮完成并保存到这个TextView的。

I have the following TextView which displays $0.00 all the time unless any calculation is done after a button click and saved to this 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)

When I run the app, it displays $0.0 (this would work, except I am working with dollars and cents and I would like it to show two digit after decimal unless both digits are 00)

我使用共享preferences ,这里是我的应用程序中的code:

I am using SharedPreferences and here are my code within the app:

的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 方法:

@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();
}

什么它该做的: 每次我打了计算按钮的应用程序应该采取什么是对的EditText etToll 并添加到值这就是已经在TextView的 tvTotalAmount ,它会继续这样做每次。

What it's suppose to do: Each time I hit the calculate button the app should take what's on the EditText etToll and add to the value thats already on the TextView tvTotalAmount and it will continue to do so everytime.

虽然TextView的原值为$ 0.00时,那只能说明$ 0.0。我想了解的EditText小数和TextView中后显示两个数字,因为我将与美元和美分的工作。

Although the original value for the TextView is $0.00, it only shows $0.0. I would like to show two digit after the decimal on the EditText and TextView because I will be working with dollar and cent.

我怎样才能解决我所面临的问题?

How can I fix the issue I am facing?

如果满code是必要的,请让我知道。

If the full code is needed, please let me know.

推荐答案

使用在code

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

将其设置为Edittext..it将工作

set it to Edittext..it will work