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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值