Wednesday, July 31, 2013

converting a C function to a floating number to string in Load Runner

I would like to call this function as ftoa

here is the code…Note: To adjust the radix/floating point position...you will have to further customize this function. 

ftoa(float floatNum, char *convFloatString) 
{
 char new[10];
 float number,dTemp,temp_val;
 int base, floatVal, radxFlag;
 char *token;
 char *temp; char cfloatVal[10],
 cBase[10]; 
 char cfloatValx[10] = "0";
 int DEBUG = 1; 
//Turn DEBUG OFF/ON by switch 0/1 
 radxFlag = 0;
 //Separate the number before and after the "." 
 number = floatNum; 
 base=number; 
 dTemp = number-base; if(DEBUG == 1) 
 { 
 lr_output_message("Base Value = %f\n", number);
 } 
 sprintf(cBase, "%d", base);
 if(DEBUG == 1) 
 { 
 lr_output_message("Floating Value = %.2f\n", dTemp);
 }
 if(dTemp == 0) //If number is a whole number then return!
 {
 lr_save_string(cBase, convFloatString); 
 return 0; 
 }
 sprintf(cfloatVal, "%.2f", dTemp);
 //Place the decimal point to suit your requirement. Default is 2
 temp = (char *)strtok(cfloatVal, "0."); 
 temp_val = atoi(temp);
 if((dTemp - 0.1) < 0) 
 radxFlag=1; 
 else
 radxFlag=0;
 if(temp_val == 0)
//If decimal values equals to 0 then return!
 { 
 strcat(cfloatVal, ".00");
 //increase the number of zero to suit your requirement. lr_save_string(cfloatVal, convFloatString); return;
 }
 if (radxFlag ==1) 
 { 
 strcat(cfloatValx,temp); strcpy(temp,cfloatValx); 
 } 
 if(DEBUG == 1) 
 {
 lr_output_message("Final decimal value = %s\n", temp); 
 } 
 if(strlen(temp) == 1 && radxFlag == 0) 
 { strcat(temp,cfloatValx); //strcpy(temp,cfloatValx); if(DEBUG == 1) 
 {
 lr_output_message("Appending a 0 %s", temp); 
 }
 } strcat(cBase, "."); strcat(cBase, temp); 
 if(DEBUG == 1) 
 {
 lr_output_message("Final decimal value = %s\n", cfloatVal); 
 }
 if(DEBUG == 1) 
 {
 lr_output_message("Final coverted floating number = %s", cBase);
 } 
 lr_save_string(cBase, convFloatString); }

Sample usage:
float floatNum; 
 floatNum = 34.102; 
 ftoa(floatNum, "convFloatStr");
 lr_output_message("Converted String = %s", lr_eval_string("{convFloatStr}"));
 return 0;

No comments: