您的位置:首页 > 其它

loadrunner实现浮点型数据转换成字符串

2015-11-08 13:56 429 查看


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);

}

Action()
{

float floatNum;

floatNum = 34.102;

ftoa(floatNum, "convFloatStr");

lr_output_message("Converted String = %s", lr_eval_string("{convFloatStr}"));

return 0;

return 0;
}


深圳湖北籍软件测试群 275212937
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: