gpt4 book ai didi

java - 如何将数字小数 (###,###,###.00) 放入字符串值中

转载 作者:行者123 更新时间:2023-12-02 13:24:00 24 4
gpt4 key购买 nike

大家好,我希望我的号码(结果)看起来像这样###,###,###.00。从下面的 Java 代码的下面的短语中进入 String.valueOf 中的 (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);
}
};

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按照您的喜好格式化string。对于整数值,请使用 #,对于小数位,请使用 0

DecimalFormat formatter = new DecimalFormat("###,###,###.00");
String resultString = formatter.format(resultat);

result.setText("the rent is " + resultString + " currency");

关于java - 如何将数字小数 (###,###,###.00) 放入字符串值中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43449895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com