【问题标题】:How can I insert a DecimalFormat in my Java Codes如何在我的 Java 代码中插入 DecimalFormat
【发布时间】:2017-04-17 17:20:59
【问题描述】:

如何在我的 Java 代码中插入 DecimalFormat。我希望我的价值(结果)像这样###、###、###.00。 (Resultat) in result.setText("租金为" + String.valueOf(resultat) + "currency");我正在尝试几种 DecimalFormat 但没有效果,因为也许我没有在字符串中正确插入 decimalFormat。谢谢你给我看。

package albencreation.realestateapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Rent extends AppCompatActivity {

    EditText price = null;
    EditText profit = null;
    TextView result = null;
    Button envoyer = null;
    Button close = null;
    Button info = null;
    Button clear = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rent);

        price = (EditText) findViewById(R.id.price);
        profit = (EditText) findViewById(R.id.profit);
        result = (TextView) findViewById(R.id.result);
        envoyer = (Button) findViewById(R.id.buttcalculate);
        close = (Button) findViewById(R.id.buttclose);
        info = (Button) findViewById(R.id.buttinfo);
        clear = (Button) findViewById(R.id.buttclear);

        envoyer.setOnClickListener(envoyerListener);
        close.setOnClickListener(closeListener);
        info.setOnClickListener(infoListener);
        clear.setOnClickListener(clearListener);
    }

    private OnClickListener envoyerListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            String p = price.getText().toString();
            String o = profit.getText().toString();

            float pValue;
            if (p.isEmpty()) {
                pValue = 0;
            } else {
                pValue = Float.valueOf(p);
            }
            float oValue;
            if (o.isEmpty()) {
                oValue = 0;
            } else {
                oValue = Float.valueOf(o);
            }

            float resultat = oValue * pValue / 100;
            result.setText("the rent is " + String.valueOf(resultat) + " currency");
        }
    };

    private OnClickListener closeListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent jumpage = new Intent(Rent.this, MainActivity.class);
            startActivity(jumpage);
            Rent.this.finish();
        }
    };

    private OnClickListener infoListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent jumpage = new Intent(Rent.this, Inforent.class);
            startActivity(jumpage);
        }
    };

    private OnClickListener clearListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            price.getText().clear();
            profit.getText().clear();
            String defaut = "result rent";
            result.setText(defaut);
        }
    };

}

【问题讨论】:

  • 很高兴您包含您的代码,但DecimalFormat 没有出现在您发布的代码中的任何位置。请在帮助中心查看Asking

标签: java android numbers decimal


【解决方案1】:

DecimalFormat 带有模式字符串的格式化属性。

DecimalFormat myFormatter = new DecimalFormat("###,###,###.00");
float resultat = oValue * pValue / 100;
result.setText("the rent is " + myFormatter.format(resultat ) + " currency");

这通过将模式字符串传递给DecimalFormat 构造函数来创建格式化程序。 format 方法接受一个双精度值作为参数,并以字符串形式返回格式化后的数字。

【讨论】:

  • 非常感谢,它可以工作了!
【解决方案2】:
result.setText("the rent is " + new DecimalFormat("###,###,###.00").format(resultat) + " currency");

我得到100,000,000.32,其值为100000000.32

【讨论】:

    【解决方案3】:

    添加额外的"###," 是多余的,###,###.00 将完成这项工作。

    DecimalFormat formatter = new DecimalFormat("###,###.00");
    

    【讨论】:

    • 谢谢,确实是多余的。
    猜你喜欢
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多