## Conversion of Values in E Notation to Decimal Numbers in CAPL

Question:

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

Background:

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 = "2.25179981368525E+015"; //string number in E notation
char numberStringCorrected; //
long pos, decimalPos;
int i, exponentNumber;
int counter = 0;
char exponentString;
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];
counter++;
}

while(counter < exponentNumber)
{
numberStringCorrected[counter] = '0';
counter++;
}

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
https://kb.vector.com/entry/1604/