Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java代码中插入十进制格式_Java_Android_Numbers_Decimal - Fatal编程技术网

如何在Java代码中插入十进制格式

如何在Java代码中插入十进制格式,java,android,numbers,decimal,Java,Android,Numbers,Decimal,如何在Java代码中插入十进制格式。我希望我的价值(结果)是这样的#########################。(Resultat)在result.setText中(“租金是”+String.valueOf(Resultat)+“货币”);我尝试了几种十进制格式,但没有效果,因为可能我没有在字符串中正确插入十进制格式。谢谢你给我看 package albencreation.realestateapplication; import android.content.Intent; imp

如何在Java代码中插入十进制格式。我希望我的价值(结果)是这样的#########################。(Resultat)在result.setText中(“租金是”+String.valueOf(Resultat)+“货币”);我尝试了几种十进制格式,但没有效果,因为可能我没有在字符串中正确插入十进制格式。谢谢你给我看

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

}

我使用模式字符串获取值为
100000000.32
100000000.32

DecimalFormat
的格式化属性

DecimalFormat myFormatter = new DecimalFormat("###,###,###.00");
float resultat = oValue * pValue / 100;
result.setText("the rent is " + myFormatter.format(resultat ) + " currency");
这将通过将模式字符串传递给
DecimalFormat
构造函数来创建格式化程序。format方法接受双精度值作为参数,并以字符串形式返回格式化的数字。

添加额外的
“###,”
是多余的,
###,###.00
将完成此工作

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

很高兴您包含了代码,但是
DecimalFormat
不会出现在您发布的代码中的任何地方。请查看帮助中心。谢谢,确实是多余的。非常感谢,它工作正常!
DecimalFormat formatter = new DecimalFormat("###,###.00");