print float up to four place on either side - c

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.

Related

what is meaning of %5d%*.*f in c language

#include <stdio.h>
int main()
{
int val =10;
float val1 = 56.40;
printf("%d%*.*f", val,val,val-8,val1);
}
output is 10 56.40
what is actual menaing of %5d%*.*f
#include <stdio.h>
int main()
{
int val =10;
float val1 = 56.40;
printf("%d%f", val,val,val-8,val1);
}
output is 100.000000
why output is getting different by just outting %d%f ?
Code containing %d%f code, which giving output as 100.0000
Code containing %d%*.*f code, which giving output as 10 56.40
%5d means print an int with a minimum field width of 5. The the value isn't 5 characters wide it will be left padded with space. You either can't tell or you didn't paste in the spaces in your example output.
%*.*f means print a float with a minimum field width and precision (digits after the .) provided as arguments as signified by the *. In this case field width is val and precision val-8.
Your 2nd code sample is simply incorrect usage. You claim to pass a int and float but pass in 3 int and a float.

I'm trying to make a function that prints a floating point numbers without using any standard library functions in C

void putfnbr(float number)
{
int a = (int) number;
putnbr(a);
write(1, ".", 1);
int i = 6;
while (i > 0)
{
number = (number - a) * (10);
a = (int) number;
putnbr(a);
i--;
}
}
putfnbr(123.456555);// output 123.456558
printf("\n%f", 123.456555); // output 123.456555
this function works well however the last number
it's converting to another number in this example:
5 becomes 8,
I want it to print the whole number as it's as the printf() dose
The problem is that the closest float value to the number 123.456555 is actually 123.4565582275390625 (0x1.edd384p+6), so that is what you get when you print it.
The printf format %f prints a double, for which the closest value is 123.4565549999999944930095807649195194244384765625 (0x1.edd38327674d1p+6) so when you print rounded to 6 decimal places (the default with %f) you get what you see.
If you change your putfnbr routine to use a double instead of a float, you'll print the value 123.456554, because you are always rounding towards zero -- you really should be rounding the last digit to the nearest integer. Unfortunately that turns out to be very hard to do while still getting all the corner cases right.
One other note -- your code will misbehave for negative numbers as written.

getting different output from different compilers

The question was:
Define a function getint(), which would receive a numeric string from keyboard, convert it to an integer number and return the integer to the calling function.
My code is:
#include<stdio.h>
#include<math.h>
#include<string.h>
int getint();
int main()
{
int a;
a = getint();
printf("you entered %d",a);
return 0;
}
int getint()
{
char str[10];
printf("Enter number: ");
gets(str);
int d=0,len = strlen(str),r = len-1;
for(int i=0;str[i] != '\0';i++,r--)
d += (str[i]-48)*pow(10,r);
return d;
}
while I run this program from sublime text or code block the output was coming wrong
output(from sublime and codeblocks):
Enter number: 123
you entered 122
But when I used onlinegdb.com/online_c_compiler the output was coming correct
So how can output differ from compiler to compiler for the same program
The pow function works with floating point numbers. As such, the result may not be exactly equal to what you expect the numerical result to be. For example, pow(10,2) could output a number slightly larger or slightly smaller than 100. If the result is just below 100, the fractional part gets truncated and you're left with 99.
Rather than using pow for an integer power, just multiply the current value of d by 10 before adding the value for the next digit.
d = 10 * d + (str[i]-'0')

Decimal precision using integer

I am programming a uC in C language and I need to show a float number with 4 precision digits. The thing here is that my number is not really a float type. I have the integer part and the decimal part of the number in two different integer variables. Let say: int digit and int decimal.
I tried using printf ("%d.%d"); That works fine when my decimal number is 6524, but the problem comes when it is exactly 65 since it doesnt show 4 decimals.
I tried using printf ("%d.%04d"); but when my decimal part is exactly 65 it shows 0065 which is not mathematically correct (I would need 6500)
I looked for any argument of printf which completes with zeros at the end but could not find anything. All of them complete with leading zeros which is not useful for me in this case.
I also though about checking if my number is minor that 10, 100 or 1000 and multiply it by 1000, 100 or 10 respectively. But it will not work when the decimal part is exactly 0, since 0*1000 will still be 0 and not 0000.
Any idea on how to solve this? Please let me know if I am not completely clear and I will provide more information
Thanks!
Since printf returns the number of characters printed, you can do it, somewhat clumsily, as follows:
printf("%d.", int_part);
int digits = printf("%d", frac_part);
while (digits++ < 4) putchar('0');
I have to say, though, that it is a very eccentric form of representing a floating point number, and you might seriously want to rethink it.
Another wired possibility is to convert the decimal part to a string and then fill it with 0:
int main() {
int i, d, len;
char p[10];
i = 189;
d = 51;
// convert the number to string and count the digits
snprintf(p, 10, "%d", d);
len = strlen(p);
while (len < 4) {p[len] = '0'; len++;}
p[len] = '\0';
fprintf(stdout, "%d.%s\n", i, p);
// you can also go back to int
d = atoi(p);
fprintf(stdout, "%d.%d\n", i, d);
}
Combine both your answers: multiply by 10, 100 or 1000 as necessary, but still print with %04d.

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);

Resources