NO

Author Topic: Control the number of digits in float  (Read 7872 times)

Manish Aggarwal

  • Guest
Control the number of digits in float
« on: September 14, 2015, 11:18:42 AM »
Why does the float display 6 digits after decimal place by default.Is it chosen by arbitrary to display 6 digits after decimal place.
How can we control the number of digits after decimal place??

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Control the number of digits in float
« Reply #1 on: September 14, 2015, 01:49:14 PM »
What is "the float display"? Can you show code?

Manish Aggarwal

  • Guest
Re: Control the number of digits in float
« Reply #2 on: September 14, 2015, 03:45:28 PM »
I think Sir you took my question in a wrong way. I try to explain you what I am asking :
If I write a program as :
Code: [Select]
#include<stdio.h>

int main()
{
float a;
a=3.56;
printf("%f",a);
}
The output of this program would be :
3.560000
I want to know why 6 digits are displayed by default in case of float.Is this chosen arbitrarily that by default 6 digits are to shown when we use float data type???
What can we do if I want my output to be 3.56 instead of 3.560000........

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Control the number of digits in float
« Reply #3 on: September 14, 2015, 08:04:57 PM »
I think Sir you took my question in a wrong way. I try to explain you what I am asking :
If I write a program as :
Code: [Select]
#include<stdio.h>

int main()
{
float a;
a=3.56;
printf("%f",a);
}
The output of this program would be :
3.560000
I want to know why 6 digits are displayed by default in case of float.Is this chosen arbitrarily that by default 6 digits are to shown when we use float data type???
What can we do if I want my output to be 3.56 instead of 3.560000........
Well, then you have to tell that the compiler... ;-)

To do that, get yourself acquainted with the options you have for the format string... ;-)

Ralf

PS: It might help to guide you in the right direction if you would tell us what source/reference you are using in order to learn C.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Control the number of digits in float
« Reply #4 on: September 15, 2015, 01:23:58 AM »
I think Sir you took my question in a wrong way.

No. You formulated your question in a totally vague way. The right question would have been

"I want to display a float variable using printf(). How can I change the format of the output?"

In this case, I would have suggested to google for c printf format specifiers float because thousands of coders have asked this question before you.

Manish Aggarwal

  • Guest
Re: Control the number of digits in float
« Reply #5 on: September 15, 2015, 04:41:52 AM »
Sorry Sir for the inconvenience.....

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Control the number of digits in float
« Reply #6 on: September 15, 2015, 04:46:59 AM »
Sorry Sir for the inconvenience.....
No inconvenience around here. It's just a very basic issue, with a very basic C functionality...

The problem why the answers you get are a bit "short" is that we never know if those questions are related to homework/projects in school. And there is pretty much an unwritten rule that nobody doesn't the homework for you.
So the answers will directly reflect the amount of info given in the questions.

Ralf

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Control the number of digits in float
« Reply #7 on: September 15, 2015, 01:29:13 PM »
Sorry Sir for the inconvenience.....

No problem. As Ralf wrote already, put more detail into your questions. Show that you've googled already, and ask only for the missing bits that google didn't give you.

And don't call me "Sir", it makes me feel terribly old (I am terribly old, but still, "jj" is enough  ;) )

tpekar

  • Guest
Re: Control the number of digits in float
« Reply #8 on: September 15, 2015, 09:55:23 PM »
printf("%4.2f",a); should do the trick.

defrancis7

  • Guest
Re: Control the number of digits in float
« Reply #9 on: September 21, 2015, 07:47:30 PM »
I think Sir you took my question in a wrong way.

No. You formulated your question in a totally vague way. The right question would have been

"I want to display a float variable using printf(). How can I change the format of the output?"

In this case, I would have suggested to google for c printf format specifiers float because thousands of coders have asked this question before you.

I remember looking for the printf() format specifiers when I first downloaded Pelles C a few years ago.  I wanted to find out how to represent long long int and long double in printf() and scanf() statements.  I went 'nuts' trying to find information in the help section. The printf() only mentioned that the common format specifers are allowed.  It did not list them, (%d, %ld, %f, %lf, %p, etc.)  I could find out how much storage each type of variable took and the minimum and maximum limits allowed.  There was no mention of how to set widths and precision, (%5d, %7ld, %5f, %-5.0f,  %-25s, etc).  At least any references that I could find!

I made the inference that one had to be an experienced C programmer, or at least knew basic C programming, before using Pelles C. I was fortunate, that as a hobby programmer,  I had some experience with Turbo C 3.1.

(Oh, I learned the specifiers for long long int, (%lld), and long double, (%Lf), while taking an online course for C++.  I must be dense, I really do not see much advantage of using C++ over C; but, then again, I mostly write simple console programs.)
-David E.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Control the number of digits in float
« Reply #10 on: September 21, 2015, 08:09:43 PM »
It helps if someone read help file.
printf leads to fprintf and formats are there.
May the source be with you

defrancis7

  • Guest
Re: Control the number of digits in float
« Reply #11 on: September 23, 2015, 07:05:08 PM »
It helps if someone read help file.
printf leads to fprintf and formats are there.
TimoVJL, I did read the help file.  I looked at printf(), scanf(), and each of the basic data types:  int, long, float, char, double, etc.  I typed "Type conversion specifiers" (and other phrases meaning essentially the same thing) into the index search.  Nothing about specifiers showed.  Neither did anything about setting widths or precision.  I have to admit that I did not look at the fprintf() family of functions.

Being just a hobby programmer, I normally do not send my program output to a file or printer on the debugging and first runs stage.  If I want to send something to a file or printer, I add the code to do so after I make sure that the program accomplishes what I intend it do. I find it simpler to do the programming that way.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Control the number of digits in float
« Reply #12 on: September 23, 2015, 10:42:18 PM »
It helps if someone read help file.
printf leads to fprintf and formats are there.
TimoVJL, I did read the help file.  I looked at printf(), scanf(), and each of the basic data types:  int, long, float, char, double, etc.  I typed "Type conversion specifiers" (and other phrases meaning essentially the same thing) into the index search.  Nothing about specifiers showed.  Neither did anything about setting widths or precision.  I have to admit that I did not look at the fprintf() family of functions.
First of all, Pelle's C is a Windows based C compiler with a Windows based IDE. It's not a C teaching tool, as a primary purpose...

Beside that, as Timo stated, all those format function are based on printf/fprintf, and the online help for that DOES state the field width etc:
Quote
The format shall be a multibyte character sequence, beginning and ending in its initial shift state. The format is composed of zero or more directives: ordinary multibyte characters (not %), which are copied unchanged to the output stream; and conversion specifications, each of which results in fetching zero or more subsequent arguments, converting them, if applicable, according to the corresponding conversion specifier, and then writing the result to the output stream.

 

Each conversion specification is introduced by the character %. After the %, the following appear in sequence:

 Zero or more flags (in any order) that modify the meaning of the conversion specification.

 An optional minimum field width. If the converted value has fewer characters than the field width, it is padded with spaces (by default) on the left (or right, if the left adjustment flag, described later, has been given) to the field width. The field width takes the form of an asterisk * (described later) or a nonnegative decimal integer (Note that zero is taken as a flag, not as the beginning of a field width).

 An optional precision that gives the minimum number of digits to appear for the d, i, o, u, x, and X conversions, the number of digits to appear after the decimal-point character for a, A, e, E, f, and F conversions, the maximum number of significant digits for the g and G conversions, or the maximum number of bytes to be written for s conversions. The precision takes the form of a period (.) followed either by an asterisk * (described later) or by an optional decimal integer; if only the period is specified, the precision is taken as zero. If a precision appears with any other conversion specifier, the behavior is undefined.

 An optional length modifier that specifies the size of the argument.

 A conversion specifier character that specifies the type of conversion to be applied.

 

As noted above, a field width, or precision, or both, may be indicated by an asterisk. In this case, an int argument supplies the field width or precision. The arguments specifying field width, or precision, or both, shall appear (in that order) before the argument (if any) to be converted. A negative field width argument is taken as a - flag followed by a positive field width. A negative precision argument is taken as if the precision were omitted.

"The answers are out there, you just have to read them..."

Ralf

henrin

  • Guest
Re: Control the number of digits in float
« Reply #13 on: December 02, 2015, 05:00:08 PM »
Thanks to all: many answers for a simple question and I guess Manish Aggarwal is satisfied.
I just can add that the C format specifiers come from the venerable FORTRAN.

In fact, I had similar questions a long time ago :
 - how can we control the total number of digits after decimal place??
 - use an engineering notation (as handheld calculators since 1972).

As you can read on http://code.activestate.com:

If you are an engineer and live in the SI world, you tend to think in kilo, mega, micro etc and tend to write big or small numbers as x = a * 10^(3*n) where n is an integer. For instance 35 535 becomes 35.5 * 10^3 and 0.00052 becomes 520 * 10^-6.

Long life to the subject!