a simple question about printf() 
Author Message
 a simple question about printf()

Hi,

I have been dong this:

        printf("%.2f", dbl_var+0.005);

But I've noticed recently that that printf()
seems to be rounding off for me once again.
Would you please testify that?

BTW.  What does the compiler do with:

        unsigned char c, d;
        long double f;

        c=f;
        d=(unsigned char)f;

Thanks in advance for any response.

--
|------------------------------------------------------------------------|
  Nan-shan Chen                    Vogeliusweg 19.4.0.3, 33100 Paderborn


|------------------------------------------------------------------------|



Wed, 29 Jan 1997 03:27:23 GMT  
 a simple question about printf()
: I have been dong this:

:       printf("%.2f", dbl_var+0.005);

: But I've noticed recently that that printf()
: seems to be rounding off for me once again.
: Would you please testify that?

According man page for printf (3S), formats f, e, E, g,
and G all round the value to the specified precision.
--

A President we deserve.                 |             Cupertino, California
A Vice-President he deserves.           | (xxx)xxx-xxxx               95013



Wed, 29 Jan 1997 13:46:29 GMT  
 a simple question about printf()
:
: Hi,
:
: I have been dong this:
:
:       printf("%.2f", dbl_var+0.005);
:
: But I've noticed recently that that printf()
: seems to be rounding off for me once again.
: Would you please testify that?

First of all, you want to print value of a variable with the precision up to
2 decimal places, why are you surprized that it is rounding off everything after
the second decimal place to print.

Second.

I assume you are using a double variable dbl_var, but you are trying to print
it as a simple float type variable.  I don't know how the compiler will react
to that.

"Mikey"



Thu, 30 Jan 1997 05:09:08 GMT  
 a simple question about printf()

Quote:
>> I have been doing this:

>>        printf("%.2f", dbl_var+0.005);

>> But I've noticed recently that that printf()
>> seems to be rounding off for me once again.
>> Would you please testify that?

|> First of all, you want to print value of a variable with the precision
|> up to 2 decimal places, why are you surprised that it is rounding off
|> everything after the second decimal place to print.

I agree with this...If the original poster only wants 2 digits precision and
is modifying the number in the 3rd decimal place then something has to
be assumed.

|> Second.
|>
|> I assume you are using a double variable dbl_var, but you are trying to
|> print it as a simple float type variable.  I don't know how the compiler
|> will react to that.

What else should he use? The %f is the correct way to print a float or a
double as I assumed it was desired to be displayed. The %lf is only
permitted when using scanf().
--





Thu, 30 Jan 1997 07:29:13 GMT  
 a simple question about printf()

Quote:
>What else should he use? The %f is the correct way to print a float or a
>double as I assumed it was desired to be displayed. The %lf is only
>permitted when using scanf().

There is no correct way to print a float with printf, because you can't
pass a float to a variadic function. The compiler will promote the
float to a double and pass the double to printf.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sun, 02 Feb 1997 00:53:45 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Simple scanf/printf question

2. replacing printf with my printf, then calling C lib's printf

3. Simple printf function

4. a simple simple question...

5. simple simple print question

6. Newbie: CATALOG sample question (SIMPLE question)

7. (void)printf() vs. printf()

8. Re(2): Re(2): (void)printf() vs. printf()

9. Re(2): (void)printf() vs. printf()

10. printf question

11. questions about sprintf, sscanf, printf, scanf, fscanf, fprintf!

12. printf() parameters question

 

 
Powered by phpBB® Forum Software