473,626 Members | 3,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

doubt regarding conversion specifier in case of printfs,sprintf s...

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 1892
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
4872
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 *emptied* after each iteration (and I could not conceive of a practical way doing that). Cheers, Matthias -- Für emails Anweisung in der Adresse befolgen
13
12573
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
8708
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"; ..... sscanf(foo,"%s%*%s%*%s%*%s%*%s",s1,s2,s3,s4,s5); <snippet/> This is giving me junk .
11
2693
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> #include <string.h> #define LENGTH 20 int temp, m, n, i, r, base10, true;
1
2479
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', but I didn't. So I guess I will have to specify some largish number there to actually print the real value, any ideas on what precision value I should use to ensure the whole thing?
26
2212
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 a register variable and this will make it much faster to access. Now the question is: obviously there are only a fixed number of registers in our CPU, maybe 6 or something. So how do we choose which
15
1527
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
2915
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 this possible.I thought it will throw "Variable redefinition Error". Giving the source code for reference.Please help me out on this... a.h ----- int g; void fun(void);
21
2440
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 type is not a single identifier or keyword. Which one is correct? For example: unsigned long x = unsigned long(y); REH
0
8266
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8199
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.