為LoadRunner寫一個lr_save_float函式

TIB發表於2010-03-28

LoadRunner中有lr_save_int() 和lr_save_string() 函式,但是沒有儲存浮點數到變數的lr_save_float函式。《lr_save_float() function for LoadRunner》這篇文章介紹瞭如何寫一個這樣的函式:

http://ptfrontline.wordpress.com/2010/01/27/lr_save_float-function-for-loadrunner/

 

 

 

 

void lr_save_float(const float value, const char *param, const int decimals)
// ----------------------------------------------------------------------------
// Saves a float into a lr variable, much like lr_save_int() saves an integer
//
// Parameters:
//   value       Float value to store
//   param       Loadrunner variable name
//   decimals    Number of decimals in the result string
//
// Returns:
//   N/A
//
// Example:
//   lr_save_float(123.456, "myVar", 2);  // myVar = 123.46 (includes rounding)
//
// ----------------------------------------------------------------------------
{
  char buf[64];                              // if more>63 digits -> your problem <IMG class=wp-smiley alt=:) src="http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif">
  char formatbuf[16];                        // 16 chars should be adequate

  sprintf( formatbuf, "%%.%df", decimals);   // Build the "%?.f" format string
  sprintf( buf, formatbuf, value);           // sprintf the value
  lr_save_string( buf, param);               // store in variable
}

 

 

 

 

 

 

使用例子如下:

#include "lr_save_float.h"

vuser_init()
{
 lr_save_float(123.456, "myVar", 2);
 lr_output_message(lr_eval_string("{myVar}"));
 return 0;
}

相關文章