I want to output a number in a string, and was using the standard way:
sprintf(title,“selection > %d”, number),
but this only outputs the nearest integer.
I tried to replace %d with %f or %lf but these only made root crash.
Any ideas on how to output the entire number and not just the nearest integer?
couet
December 8, 2014, 10:41am
2
It depends what “number” is. If you want to use %g or %f “number” must be a float or a double. Better cast it is you are not sure what is it. Here is a small example :
void print_pi()
{
// 1st way:
char pi_string[20];
double pi = 3.14159;
sprintf(pi_string, "pi = %g",(double)pi);
printf("%s\n", pi_string);
//2nd way:
printf("%s\n", Form("pi = %g",(double)pi));
}
You’d better use snprintf instead of sprintf: {
char title[100] = "some old value";
std::cout << "title = " << title << std::endl;
double number = 1.234567;
snprintf(title, sizeof(title), "selection > %g", number);
std::cout << "title = " << title << std::endl;
}