我试图在android studio中将十进制数分成整数和点Button btSpilt;EditText Input;TextView wholenumber, points;@Overrideprotected 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); } });输入整数时效果很好,但输入小数时整个应用程序崩溃
2 回答

手掌心
TA贡献1942条经验 获得超3个赞
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;

临摹微笑
TA贡献1982条经验 获得超2个赞
int a = Integer.parseInt(s_input);//this line caused error when s_input represents decimal.
double b = (10 * input - 10 * input)/10;//b always equal zero
要获得双精度数的整数部分和小数部分,您可以尝试使用split
String s_input = Input.getText().toString();
if (s_input.contains(".")) {
String[] split = s_input.split("\\.");
String whole = split[0];
String fractional = split[1];
}
添加回答
举报
0/150
提交
取消