472,791 Members | 1,844 Online

# 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 1844
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 thread has been closed and replies have been disabled. Please start a new discussion.