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

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
|
作者:Agoly 出处:https://www.cnblogs.com/qmfsun/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |

浙公网安备 33010602011771号