Java - 无法在基本类型int上调用padLz(int)

时间:2014-03-04 01:35:39

标签: java javascript android eclipse android-activity

我一直试图移植一段JavaScript代码,现在,我还没有开始工作的唯一部分就是这个错误: 无法在原始类型int上调用padLz(int) 即可。最初的JavaScript代码在下面,同样是我的移植版本,其中我尚未能够突出显示的部分。

错误:

无法在基本类型int

上调用padLz(int)

有没有人知道修复?

原始

 OsGridRef.prototype.toString = function(digits) {
              digits = (typeof digits == 'undefined') ? 10 : digits;
              e = this.easting, n = this.northing;
              if (e==NaN || n==NaN) return '??';

              // get the 100km-grid indices
              var e100k = Math.floor(e/100000), n100k = Math.floor(n/100000);

              if (e100k<0 || e100k>6 || n100k<0 || n100k>12) return '';

              // translate those into numeric equivalents of the grid letters
              var l1 = (19-n100k) - (19-n100k)%5 + Math.floor((e100k+10)/5);
              var l2 = (19-n100k)*5%25 + e100k%5;

              // compensate for skipped 'I' and build grid letter-pairs
              if (l1 > 7) l1++;
              if (l2 > 7) l2++;
              var letPair = String.fromCharCode(l1+'A'.charCodeAt(0), l2+'A'.charCodeAt(0));

              // strip 100km-grid indices from easting & northing, and reduce precision
              e = Math.floor((e%100000)/Math.pow(10,5-digits/2));
              n = Math.floor((n%100000)/Math.pow(10,5-digits/2));

              var gridRef = letPair + ' ' + e.padLz(digits/2) + ' ' + n.padLz(digits/2);

              return gridRef;
            }

 // pad a number with sufficient leading zeros to make it w chars wide

    Number.prototype.padLZ = function(w) {
      var n = this.toString();
      for (var i=0; i<w-n.length; i++) n = '0' + n;
      return n;
    }

移植版本

  public String gridrefNumToLet(int e, int n, int digits) {

  // get the 100km-grid indices
  double e100k = Math.floor(e/100000), n100k = Math.floor(n/100000);

  if (e100k<0 || e100k>6 || n100k<0 || n100k>12) return null;

  // translate those into numeric equivalents of the grid letters
  double l1 = (19-n100k) - (19-n100k)%5 + Math.floor((e100k+10)/5);
  double l2 = (19-n100k)*5%25 + e100k%5;

  // compensate for skipped 'I' and build grid letter-pairs
  if (l1 > 7) l1++;
  if (l2 > 7) l2++;

  String letPair = String.valueOf(l1+"A".charAt(0))+ String.valueOf(l2+"A".charAt(0));

  // strip 100km-grid indices from easting & northing, and reduce precision
  e = (int) Math.floor((e%100000)/Math.pow(10,5-digits/2));
  n = (int) Math.floor((n%100000)/Math.pow(10,5-digits/2));

字符串gridRef = letPair +''+ e.padLz(digits / 2)+''+ n.padLz(digits / 2);

  return gridRef;
}

 // pad a number with sufficient leading zeros to make it w chars wide

    public String padLZ(String w) {
              String n = this.toString();
              for (int i=0; i< n.length(); i++) n = '0' + n;
              return n;
    }

2 个答案:

答案 0 :(得分:0)

用padLz(e,digits / 2)和padLz(n,digits / 2)替换e.padLz(digits / 2)和n.padLz(digits / 2),然后实现带签名的方法:< / p>

String padLz(int val, int digits) {
    ...
}

答案 1 :(得分:0)

将原始int值作为参数传递给padLz函数。

作为

String padLz(int val, int digits) {

然后考虑使用

org.apache.commons.lang3.StringUtils.leftPad("" + val, digits, '0');

中的变体

How can I pad an integers with zeros on the left?

示例

private static String padLz (int val, int digits) {
    return org.apache.commons.lang3.StringUtils.leftPad("" + val, digits, '0');
}

System.out.println(padLz (2, 5));    --> 00002