Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "%f" to populate a double value into a string with the right precision

I am trying to populate a string with a double value using a sprintf like this:

sprintf(S, "%f", val);

But the precision is being cut off to six decimal places. I need about 10 decimal places for the precision.

How can that be achieved?

like image 494
AMM Avatar asked Sep 16 '08 06:09

AMM


People also ask

How do you set a precision to a double in C++?

The C++ setprecision can also be used to format only the decimal places instead of the whole floating-point or double value. This can be done using the fixed keyword before the setprecision() method.

What is %d for double in C?

%d stands for decimal and it expects an argument of type int (or some smaller signed integer type that then gets promoted). Floating-point types float and double both get passed the same way (promoted to double ) and both of them use %f .


1 Answers

%[width].[precision]

Width should include the decimal point.

%8.2 means 8 characters wide; 5 digits before the point and 2 after. One character is reserved for the point.

5 + 1 + 2 = 8

like image 170
Agnel Kurian Avatar answered Sep 24 '22 11:09

Agnel Kurian