Changeset 3053 in webkit for trunk/JavaScriptCore/kjs


Ignore:
Timestamp:
Dec 14, 2002, 7:33:10 PM (22 years ago)
Author:
mjs
Message:

Reviewed by Ken.

  • further corrections to number printing.
  • kjs/ustring.cpp: (UString::from): Make number printing match the ECMA standard algorithm.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/ustring.cpp

    r3051 r3053  
    337337  int decimalPoint;
    338338  int sign;
    339 
    340   char *result = kjs_dtoa(d, 5, 16, &decimalPoint, &sign, NULL);
     339 
     340  char *result = kjs_dtoa(d, 0, 0, &decimalPoint, &sign, NULL);
    341341  int length = strlen(result);
    342342 
     
    346346  }
    347347 
    348   if (decimalPoint <= 0) {
     348  if (decimalPoint <= 0 && decimalPoint > -6) {
    349349    buf[i++] = '0';
    350350    buf[i++] = '.';
     
    353353    }
    354354    strcpy(buf + i, result);
    355   } else if (decimalPoint >= length) {
     355  } else if (decimalPoint <= 21 && decimalPoint > 0) {
     356    if (length <= decimalPoint) {
     357      strcpy(buf + i, result);
     358      i += length;
     359      for (int j = 0; j < decimalPoint - length; j++) {
     360        buf[i++] = '0';
     361      }
     362      buf[i] = '\0';
     363    } else {
     364      strncpy(buf + i, result, decimalPoint);
     365      i += decimalPoint;
     366      buf[i++] = '.';
     367      strcpy(buf + i, result + decimalPoint);
     368    }
     369  } else if (result[0] < '0' || result[0] > '9') {
    356370    strcpy(buf + i, result);
    357371  } else {
    358     strncpy(buf + i, result, decimalPoint);
    359     i += decimalPoint;
    360     buf[i++] = '.';
    361     strcpy(buf + i, result + decimalPoint);
    362   }
     372    buf[i++] = result[0];
     373    if (length > 1) {
     374      buf[i++] = '.';
     375      strcpy(buf + i, result + 1);
     376      i += length - 1;
     377    }
     378   
     379    buf[i++] = 'e';
     380    buf[i++] = (decimalPoint >= 0) ? '+' : '-';
     381    // decimalPoint can't be more than 3 digits decimal given the
     382    // nature of float representation
     383    int exponential = decimalPoint - 1;
     384    if (exponential < 0) {
     385      exponential = exponential * -1;
     386    }
     387    if (exponential >= 100) {
     388      buf[i++] = '0' + exponential / 100;
     389    }
     390    if (exponential >= 10) {
     391      buf[i++] = '0' + (exponential % 100) / 10;
     392    }
     393    buf[i++] = '0' + exponential % 10;
     394    buf[i++] = '\0';
     395  }
     396 
    363397  kjs_freedtoa(result);
    364398 
Note: See TracChangeset for help on using the changeset viewer.