2

I have to print a floating point value, however the precision is not known at compile time. So this parameter has to be passed as agument. How this can be achieved. In windows, with CString, the format function helps to achieve this. How to achieve this without CString. Code:

int main()
{

   /* This below code segment works fine.*/
   char str[80];
   sprintf(str, "Value of Pi = %.3f", 3.147758);
   std::cout << str << std::endl; //Prints "Value of Pi = 3.148"

   /* When the precision may vary, henc It needs to be printed required on that.*/
   char str2[80];
   std::string temp = "%.3f";   //This is required as the precision may change. 
                                    // i.e I may have 4 instead 3 decimal points.
   sprintf(str2, "Value = %s", temp, 3.148257);
   std::cout << str2 << std::endl;  //Prints "Value = <null>"
   return 0;
}
4
  • 3
    Are you sure you need to use sprintf? This is easy with streams. Commented Apr 29, 2014 at 9:48
  • Have you tried * for width? Commented Apr 29, 2014 at 9:48
  • Have you found appropriate solution? If so, you should accept the most useful answer. Commented Aug 5, 2014 at 12:14
  • what about using %g or %G? -> sprintf(str, "Value of Pi = %g", 3.147758); Commented Oct 16, 2024 at 12:12

5 Answers 5

6

You need the

"%.*f"

syntax, where the "*" refers to the next argument.

This is all documented.

Sign up to request clarification or add additional context in comments.

Comments

1

If I understand your question right, you can use setPrecision(..) to achieve desired result:

#include <iostream>
#include <iomanip> // for setprecision()
int main()
{
    using namespace std;

    cout << setprecision(16); // show 16 digits
    float fValue = 3.33333333333333333333333333333333333333f;
    cout << fValue << endl;
    double dValue = 3.3333333333333333333333333333333333333;
    cout << dValue << endl;
}

Comments

1

You can use std::stringstream to convert float to std::string. If you want to use sprint. than use * for width. Details here.

Comments

0

If you can use iomanip then do this:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int numOfDecimals = 2;// this controls precision. change to what ever you want
    float PI = 3.147758;

    cout << fixed << setprecision(numOfDecimals);
    cout << PI << endl;
    return 0;
}

Comments

0

if you wanted to find the amount of decimal places on the fly, you could do something like the following which counts the the amount of decimal places. This would mean you do not have to pass in the format string at all.

#include <iostream>
using namespace std;

int main() 
{
double dNum = 3.14159265359, threshold = 0.00000000005;
double temp = dNum;
int count = 0;
while((temp - (int)temp) > threshold) 
{
    temp *= 10;  
    threshold *=10;
    count++;              
} 
char buffer[50];
sprintf(buffer,"value is = %.*f",count,dNum);
return 0;
}

2 Comments

For most of the numbers, while will loop infinitely.
fixed - although I had to limit it to 10 decimal places.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.