469,939 Members | 2,378 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,939 developers. It's quick & easy.

doubt regarding conversion specifier in case of printfs,sprintfs...

Consider the following code snippet
------------------
float fi=12.598;
printf("\n %.2f \n",fi);
------------------

the output of this would be 12.60 (note when we give the conversion
specifier as "%.2f" the result is rounded to 12.60)

My requirement is the value be 12.59 instead of 12.60.Are there any
conversion specifier that suppress this rounding

Feb 7 '07 #1
2 1672
ra*************@gmail.com wrote:
Consider the following code snippet
------------------
float fi=12.598;
printf("\n %.2f \n",fi);
------------------

the output of this would be 12.60 (note when we give the conversion
specifier as "%.2f" the result is rounded to 12.60)

My requirement is the value be 12.59 instead of 12.60.Are there any
conversion specifier that suppress this rounding
No. You'll have to do the truncation yourself:

float fi = 12.598;
fi = floor(100 * fi) / 100;
printf("\n %.2f \n",fi);

--
Clark S. Cox III
cl*******@gmail.com
Feb 7 '07 #2
ra*************@gmail.com wrote:
Consider the following code snippet
------------------
float fi=12.598;
printf("\n %.2f \n",fi);
------------------

the output of this would be 12.60 (note when we give the conversion
specifier as "%.2f" the result is rounded to 12.60)

My requirement is the value be 12.59 instead of 12.60.Are there any
conversion specifier that suppress this rounding
No, there is no conversion specifier to suppress rounding, and are you
sure you want to, even if you get 12.599999999999? And do you always
want to round down, or do you always want to truncate? In other words,
should -12.598 be printed as -12.59, or as -12.60? Depending on your
needs, you can add or subtract 0.005 or slightly less, and then print
the result of that.

Feb 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Der Andere | last post: by
13 posts views Thread by sam | last post: by
12 posts views Thread by Simone Mehta | last post: by
26 posts views Thread by Vashna | last post: by
15 posts views Thread by somenath | last post: by
11 posts views Thread by whirlwindkevin | last post: by
21 posts views Thread by REH | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.