所有-我正在开发一个应用程序,其中用户输入账单费用小费费率。我知道如何获取edittext字段的值并对其执行必要的数学运算,但在tip rate字段中,我必须在输入的数字前面添加一个小数点。因此,如果用户输入"2“,它在java代码中显示为".2”。有没有办法做到这一点?在进阶时谢谢!
发布于 2012-06-01 23:47:47
很简单-
如果你想在字符之间加一个点,只需将其除以10^(长度)即可。因此,如果输入的字符串是2,除以10。如果是22,则除以100。
另一种选择是-
String value = editText.getText().toString();
if(!value.contains(".")) //Better if you check the value doesn't already have a decimal
value = "."+value; //This is the string with . in the front
float tip = Float.parseFloat(value); //Use it for calculations.https://stackoverflow.com/questions/10853374
复制相似问题