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
", number);
	}

	sprintf(cBase, "%d", base);

	if(DEBUG == 1){

		lr_output_message("Floating Value = %.2f
", 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
", 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
", 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

原文地址:https://www.cnblogs.com/qmfsun/p/4947214.html