473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 1878
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.

Similar topics

16
by: Der Andere | last post by:
During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using stringstreams. However, the stream would have to be...
13
by: sam | last post by:
Hi, Does STL has Integer class? I want to convert an integer to string. Of course I can use sprintfs C API, but I m wondering whether STL has Integer class to be utilsed. Thanks Sam.
12
by: Simone Mehta | last post by:
hi All, I am parsing a CSV file. I want to read every row into a char array of reasonable size and then extract strings from it. <snippet> char foo="hello,world,bye,bye,world"; ........
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
1
by: Benny the Guard | last post by:
Using snprintf and sprintfs to print a double. I need the highest level of precision apparently snprintf/sprintf defaults to a precision of 6. If I wanted a precision of 6 I would have done '%.6f',...
26
by: Vashna | last post by:
Hi Group, I have a doubt about register variables. I know that if we have a variable used very frequently in a function, then provided we never apply the & function to it, we can define it as...
15
by: somenath | last post by:
Hi All, I am not able to understand the behavior of the bellow mentioned program. #include<stdio.h> int main(void) { printf("\n Size = %d \n",sizeof 1<0); return 0;
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
21
by: REH | last post by:
It it permissible to use the constructor style cast with primitives such as "unsigned long"? One of my compilers accepts this syntax, the other does not. The failing one chokes on the fact that the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.