Java 文本中的有效数字
我有一个Java 文本中的有效数字,java,android,Java,Android,我有一个EditText,用户在其中输入一个数字 有没有办法设定有效数字? 因此,它会在3.s.f.时自动将“003645”更改为“365”,如下所示: // input, you have to read your EditText String input = "003645"; // your example int significantFigures = 3; // your example // calculation int number = Integ
EditText
,用户在其中输入一个数字
有没有办法设定有效数字?
因此,它会在3.s.f.时自动将“003645”
更改为“365”
,如下所示:
// 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
这将为输入
001000
和3
有效数字生成1000
。如果它应该为相同的输入和大量的数字生成100
,则需要在while
条件下使用更大或相等的数字。文本字段可以设置为密码、文本、数字等。。。如果需要从输入中删除某些部分,唯一的方法就是使用java方法。