| 401 | |
| 402 | private static Pattern EXP_PATTERN = Pattern.compile("^(.*)e([+-])([0-9]+)$"); |
| 403 | public static String toString(ExecutionContext context, Object o) { |
| 404 | if (o == Types.UNDEFINED) { |
| 405 | return "undefined"; |
| 406 | } |
| 407 | |
| 408 | if (o == Types.NULL || o == null) { |
| 409 | return "null"; |
| 410 | } |
| 411 | |
| 412 | if (o instanceof JSObject) { |
| 413 | return (String) toString(context, toPrimitive(context, o, "String")); |
| 414 | } |
| 415 | if (o instanceof Number) { |
| 416 | if (((Number) o).doubleValue() == -0) { |
| 417 | return "0"; |
| 418 | } |
| 419 | |
| 420 | if (o instanceof Double) { |
| 421 | Double dbl = (Double) o; |
| 422 | if (dbl.doubleValue() == (long) dbl.doubleValue()) { |
| 423 | o = dbl.longValue(); |
| 424 | } |
| 425 | } |
| 426 | String result = Types.rewritePossiblyExponentialValue(o.toString()); |
| 427 | |
| 428 | Matcher matcher = EXP_PATTERN.matcher(result); |
| 429 | if (matcher.matches()) { |
| 430 | int expValue = Integer.parseInt(matcher.group(3)); |
| 431 | if (matcher.group(2).equals("+")) { |
| 432 | if (expValue <= 20) { |
| 433 | String digits = matcher.group(1).replace(".", ""); |
| 434 | StringBuilder newResult = new StringBuilder(); |
| 435 | int offset = 0; |
| 436 | if (digits.startsWith("-")) { |
| 437 | newResult.append("-"); |
| 438 | offset = 1; |
| 439 | } |
| 440 | for (int i = offset; i <= expValue; ++i) { |
| 441 | if (i >= digits.length()) { |
| 442 | newResult.append("0"); |
| 443 | } else { |
| 444 | newResult.append(digits.charAt(i)); |
| 445 | } |
| 446 | } |
| 447 | if (newResult.length() < digits.length()) { |
| 448 | newResult.append("."); |
| 449 | newResult.append(digits.substring(newResult.length() - 1)); |
| 450 | } |
| 451 | result = newResult.toString(); |
| 452 | } |
| 453 | } else { |
| 454 | if (expValue <= 6) { |
| 455 | StringBuilder newResult = new StringBuilder().append("0."); |
| 456 | for (int i = 0; i < (expValue - 1); ++i) { |
| 457 | newResult.append(0); |
| 458 | } |
| 459 | |
| 460 | String digits = matcher.group(1).replace(".", ""); |