choose how many numbers to print after dot using a variable - c

Lets say for example I want to print result 3 numbers after the dot.
I can just use the following code:
printf("%.3f",result);
I want to use it using the variable "precision" instead of writing 3
printf("%.precisionf");
how do i do that correctly

Use the somewhat forgotten ".*" format:
printf("%.*f", precision, result);
where precision is an int. Full program:
#include <stdio.h>
int main(void) {
double result = 1.234567;
int precision = 3;
printf("%.*f", precision, result);
}
See https://ideone.com/WFimaU

You can do that with sprintf to create the format string in the following way:
char tmp[10];
sprintf(tmp,"%%.%df",precision);
printf(tmp, result);
Simple, don't?

Related

Expected Expression with square roots

#define PI = 3.141593
#define G = 6.67259E-11
#define g = 9.80665
#define M = 5.972E+24
#define r = 6378000
#define h = 220
#include <stdio.h>
#include <math.h>
int main(void)
{
int value;
value =sqrt((G/M)/(r+h))
printf("This is the tangential speed:") value;
return 0;
}
I am very new to coding, and my program is giving me several errors in code blocks, can anyone give me some guidance?
Remove the = from all the #define statements. They are preprocessor macro definitions, not assignment statements, and they do not use equal signs.
Change int value to double value, to use floating-point instead of integers.
Add a ; after value =sqrt((G/M)/(r+h)). Statements in C generally end with a semicolon.
Change printf("This is the tangential speed:") value; to printf("This is the tangential speed: %g.\n", value);. printf is a function call, not a statement, so you pass everything it needs inside a set of parentheses. The string is a format string; it contains both literal text you want printed and conversion specifications like %g that tell it to convert an argument to a string. %g tells it to convert a double argument to a general floating-point display form.
I see two problems:
Smallest problem, but might be significant, is that I assume you want value to be a float or double instead of an int, thus replace
int value;
by
float value;
The print statement is incorrect:
printf("This is the tangential speed:") value;
Assuming value is a float, change it to
printf("This is the tangential speed: %f\n", value);
\n makes a new line.
And of course don't forget the remark by chux.

print float up to four place on either side

I am newbie, I am trying to print an float value upto four place on either side. For example 11.3 will be 0011.3000. For this I am using following line:-
float t = 11.3;
printf("%4.4f", t);
But I am getting 11.3000. So, is it even possible what i am trying to do? If yes then how? Thanks
The type of f is incorrect in your code fragment, it should be float or double.
To produce leading zeroes, use the 0 printf modifier and specify the minimum width expected, 9 characters in your example (4 places before the . plus the . plus 4 more places after the .).
The format is therefore %09.4f.
Here is the code:
#include <stdio.h>
int main() {
double t = 11.3;
printf("%09.4f\n", t);
return 0;
}
Output:
0001.3000
Note however that if the number is negative, you will only get 3 places before the . and if the number is too large in absolute value (<= -999.99995 or >= 9999.99995), the output will have more than 9 characters. If you mean to have 4 places before the . for negative values too, you should use % 010.4f: the number will then be prefixed with a space if positive and a - if negative:
#include <stdio.h>
int main() {
double t = 11.3;
printf("%09.4f\n", t);
printf("% 010.4f\n", t);
printf("% 010.4f\n", -t);
return 0;
}
Output:
0011.3000
0011.3000
-0011.3000
float t = 11.3;
printf("%4.4f", t);
In your code above 4 before . means total number of characters to be printed.
and 4 after . mean number of characters after decimal.
Since you want xxxx.xxxx hence you should write it like:
printf("%9.4f", t);
The first part of the format spec is the width of the field you are printing in, not the number of digits before the decimal place. Your desired output has 9 characters in it, zero padded on the left, so do
float t = 11.3;
printf("%09.4f", t);
This might break down if for example the integer part of your number gets too big For a finer level of control, work with integers:
float t = 11.3;
int i, f;
i = (int)t;
f = (int)((t - i) * 10000 + 0.5);
printf("%04d.%04d", i, f);
All this assumes positive numbers. Neither example shown here will work properly with negatives.

How to specify the number of digits to be outputted after decimal point [duplicate]

I wrote a small program that reads two integers using scanf and then performs various arithmetic calculations. I'm using printf to display the results. How can I make printf display only two digits after the decimal point? Starting with the simplified code sample:
#include <stdio.h>
int main(void)
{
double third = 1.0 / 3.0;
// display data
printf("\n%20s%20s", "Description", "Data");
printf("\n%20s%20s", "-----------", "----");
printf("\n%20s%20lf", "One third", third);
printf("\n");
return 0;
}
This prints "0.333333" for the value of third. How would I alter the above to get the following output?
Description Data
----------- ----
One third 0.33
use "%.2f" at the place you want.
For example, modify the following statement
printf("\n%20s%20lf", "Fraction", quotientdecimal);
into this one :
printf("\n%20s%.2f", "Fraction", quotientdecimal);
will only display two fraction numbers of the variable quotlentdecimal.

How to format a float number to the right in C?

I'd like to print a float number with only 2 decimal places and align it to the right in a 6 characters space.
I tried to do this, but didn't work:
printf("%6.2f", value);
What you've posted will fit the whole float into a 6 char wide column with the .xx taking up the last 3. If you want the integer portion in a six char wide column with the '.' and the fractional portion after these 6 characters, its %9.2f. Quick example program to show the differences
#include <stdio.h>
int main(void) {
float x = 83.4;
printf("....|....|....|\n");
printf("%6.2f\n", x); // prints " 83.40"
printf("%9.2f\n", x); // prints " 83.40"
return 0;
}
And the output:
....|....|....|
83.40
83.40
You have to put - after %
printf("%-6.2f", value);

How can I limit the number of digits displayed by printf after the decimal point?

I wrote a small program that reads two integers using scanf and then performs various arithmetic calculations. I'm using printf to display the results. How can I make printf display only two digits after the decimal point? Starting with the simplified code sample:
#include <stdio.h>
int main(void)
{
double third = 1.0 / 3.0;
// display data
printf("\n%20s%20s", "Description", "Data");
printf("\n%20s%20s", "-----------", "----");
printf("\n%20s%20lf", "One third", third);
printf("\n");
return 0;
}
This prints "0.333333" for the value of third. How would I alter the above to get the following output?
Description Data
----------- ----
One third 0.33
use "%.2f" at the place you want.
For example, modify the following statement
printf("\n%20s%20lf", "Fraction", quotientdecimal);
into this one :
printf("\n%20s%.2f", "Fraction", quotientdecimal);
will only display two fraction numbers of the variable quotlentdecimal.

Resources