0

我有一个EditText用户输入数字的地方。

有没有办法设置有效数字?所以它会自动更改"003645""365"3.sf

4

1 回答 1

0

像这样:

  // 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输入0010003重要的数字。如果它应该产生100相同的输入和大量的数字,则需要在while条件中使用更大或等于。

于 2013-01-06T11:56:14.080 回答