我有一个EditText
用户输入数字的地方。
有没有办法设置有效数字?所以它会自动更改"003645"
为"365"
3.sf
像这样:
// input, you have to read your EditText
String input = "003645"; // your example
int significantFigures = 3; // your example
// calculation
int number = Integer.parseInt(input, 10);
int tooBig = (int) Math.pow(10, significantFigures);
while (number > tooBig) {
number = (int) Math.round(number / 10.0);
}
System.out.println(number); // prints 365
这将产生1000
输入001000
和3
重要的数字。如果它应该产生100
相同的输入和大量的数字,则需要在while
条件中使用更大或等于。