gpt4 book ai didi

java - 在android studio中将 double 分成整数和小数部分

转载 作者:行者123 更新时间:2023-12-02 10:43:39 25 4
gpt4 key购买 nike

我正在尝试在android studio中将小数分成整数和点

Button btSpilt;
EditText Input;
TextView wholenumber, points;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

wholenumber = (TextView) findViewById(R.id.wholenumber);
points = (TextView) findViewById(R.id.points);
Input = (EditText) findViewById(R.id.Input);
btSpilt = (Button) findViewById(R.id.btSpilt);

btSpilt = findViewById(R.id.btSpilt);
btSpilt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String s_input = Input.getText().toString();

double input = Double.parseDouble(s_input);
int a = Integer.parseInt(s_input);
double aa = a;
double b = (10 * input - 10 * input)/10;

String str_a = Integer.toString((int) aa);
String str_b = Integer.toString((int) b);

wholenumber.setText(str_a);
points.setText(str_b);
}
});

输入整数时效果很好,但输入小数时整个应用程序崩溃

最佳答案

Math.floor() 将对此有所帮助: https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#floor(double)

        double input = Double.parseDouble(s_input);
double wholeNumber = Math.floor(input);
double afterDecimal = input - wholeNumber;

关于java - 在android studio中将 double 分成整数和小数部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52749563/

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