【问题标题】:How to put a number decimal (###,###,###.00) in a string value如何将数字小数(###,###,###.00)放入字符串值
【发布时间】:2017-04-17 10:42:20
【问题描述】:

大家好,我希望我的号码(结果)看起来像这样 ###,###,###.00。从以下 Java 代码的以下短语进入 String.valueOf 中的(结果)。感谢您的回答。 result.setText("租金为" + String.valueOf(resultat) + "currency");

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

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

}

【问题讨论】:

    标签: java android numbers decimal number-formatting


    【解决方案1】:

    根据需要使用DecimalFormat 格式化string。对于整数值使用#,对于小数位使用0

    DecimalFormat formatter = new DecimalFormat("###,###,###.00");
    String resultString = formatter.format(resultat);
    
    result.setText("the rent is " + resultString + " currency");
    

    【讨论】:

    • 对不起,但这告诉我 String resultat = formatter.format(resultat);最后一个词(结果)是红色的,带有非法自我引用?
    • 现在检查!我错过了你的变量是同名的! :)
    【解决方案2】:

    试试这个:

    import java.text.DecimalFormat;
    float f = YourValue;
    DecimalFormat decimalFormat = new DecimalFormat("#.##");
    float twoDigitsF = Float.valueOf(decimalFormat.format(f));
    

    此节目代码Two Dights希望对您有所帮助

    【讨论】:

    • 您的回答有误。 #.## 应该是 #.00 两位数
    • 感谢您的观察,但“#.##”表示它将打印最多两位小数的数字,而“#.00”表示它将始终显示两位小数
    • 像这样有可能###,###,###.00,它是什么(f)?
    • float f f 是你的变量
    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    相关资源
    最近更新 更多