diff --git a/classpath/java/lang/Long.java b/classpath/java/lang/Long.java index 76366be3ac..e1445ec5d9 100644 --- a/classpath/java/lang/Long.java +++ b/classpath/java/lang/Long.java @@ -128,15 +128,19 @@ public final class Long extends Number implements Comparable { int i = 0; long number = 0; boolean negative = s.startsWith("-"); + int length = s.length(); if (negative) { i = 1; + -- length; } + long factor = pow(radix, length - 1); for (; i < s.length(); ++i) { char c = s.charAt(i); int digit = Character.digit(c, radix); if (digit >= 0) { - number += digit * pow(radix, (s.length() - i - 1)); + number += digit * factor; + factor /= radix; } else { throw new NumberFormatException("invalid character " + c + " code " + (int) c); diff --git a/test/Misc.java b/test/Misc.java index 5be739c5d5..be83804e78 100644 --- a/test/Misc.java +++ b/test/Misc.java @@ -102,6 +102,10 @@ public class Misc { expect(foo > 0); } + expect(Long.parseLong("25214903884") == 25214903884L); + + expect(Long.parseLong("-9223372036854775808") == -9223372036854775808L); + expect(String.valueOf(25214903884L).equals("25214903884")); expect(String.valueOf(-9223372036854775808L).equals