Conversion of Values in E Notation to Decimal Numbers in CAPL


How to convert string number in E notation to number in CAPL.


In CAPL large numbers may be displayed in E notation in logs, trace and measurement values. Sometimes it may be required to convert these values read as string to numbers for programming purpose. The CAPL functions atoi64(), atodbl() and atoll() cannot convert a string number in E notation to number. To compensate for this, the function below can be used.


Please see the CAPL code below:

 on key 's'
char numberString[100] = "2.25179981368525E+015"; //string number in E notation  
char numberStringCorrected[100]; //
  long pos, decimalPos;
  int i, exponentNumber;
  int counter = 0;
  char exponentString[510];
  int64 numberNumber;
  pos = mbstrstr(numberString,"E+");
  decimalPos = mbstrstr(numberString,".");
  write("Position is %d", pos);
  write("Total No of Characters is %d", strlen(numberString));
  write("No of Characters to copy is %d", strlen(numberString) - pos - 2);
  substr_cpy(exponentString, numberString, pos + 2, 4,          elcount(exponentString));
  write("exponent string is %s", exponentString);
  exponentNumber = atol(exponentString);
  write("exponent number is %d", exponentNumber);
  for(i = 0; i < decimalPos; i++)
    numberStringCorrected[i] = numberString[i];
  for(i = decimalPos; i < pos -1; i++)
    numberStringCorrected[i] = numberString[i + 1];
  while(counter < exponentNumber)
    numberStringCorrected[counter] = '0';
  write("numberStringCorrected is %s",numberStringCorrected);
  numberNumber = _atoi64(numberStringCorrected);
  write("numberNumber is %I64d",numberNumber); 


Article ID: 1604
Last updated: 2020-02-06
Revision: 3
CANalyzer/CANoe -> CAPL -> Conversion of Values in E Notation to Decimal Numbers in CAPL