473,748 Members | 10,539 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf - format command of C that will not round of the value

1 New Member
i have a question
please refer below..

Example:

#include <stdio.h>
int main() {
printf("%6.1f", 123456.123456);
printf("\n%6.2f ", 123456.123456);
printf("\n%6.3f ", 123456.123456);
printf("\n%6.4f ", 123456.123456);
}

output :
123456.1
123456.12
123456.123
123456.1235

at last output the result was 123456.1235 because it was rounded up.

How to display this value by not rounding up?
like 123456.1234 ?
May 29 '08 #1
1 3743
arnaudk
424 Contributor
Formatted string conversions will always round as far as I know. The only way would be to truncate all decimals beyond the 4th:
Expand|Select|Wrap|Line Numbers
  1. #include <math.h>
  2. double truncate(const double val, const int d)
  3.     double rval = val * pow(10,d);
  4.     rval = (double) floor(rval);
  5.     return rval / pow(10,d);
  6. }
  7.  
  8. int main()
  9. {
  10.     double r =1.23456;
  11.     printf("r = %f, r_4 = %1.4f, r_4t = %1.4f\n",r,r,truncate(r,4));
  12.     return 0;
  13. }
  14.  
  15. // Result: r = 1.23456, r_4 = 1.2346, r_4t = 1.2345
  16.  
May 29 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
15475
by: Mike MacSween | last post by:
This looks like a bug to me. I have an expression on a report: =Format(Sum((**)*(/)),"0.00") Score is byte PercentOfGrade is double PropDegree is single ModuleCats is byte
11
5950
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <= 4 */ };
188
17405
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
23
8743
by: Matt Garman | last post by:
Is there a clean, portable way to determine the maximum value of converted numerical fields with printf()-like functions? Doing this at compile-time would be preferable. For example, %i should never convert to a string that is longer than the number of digits in INT_MAX, right? I'd like to create a structure that contains character representations of numerical types (e.g., instead of storing 3421 as an int, it would store the char...
12
7628
by: John Devereux | last post by:
Hi, I would like to know if their is a conversion specifier, or other method, that will allow me to not use particular arguments to printf. Failing that, is it permissable to simply "not use" the parameter if it is at the end of the parameter list? For example,
4
1739
by: hamster | last post by:
I don't know how to write the code for round command? For example if more than .5 then round up,if less than .5 round down such as 4.3 round to 4.0 and 5.5 round to 6.0
19
5463
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
11
2676
by: vectorizor | last post by:
Hi guys, My program deals with with lots of large matrices. To easily debug it, I would like to be able to dump them in files, so that I can easily import then in other software (think matlab, excel) so I can analyse the intermediate results. To make sure the data can be understood by other programs, a space need to be included between each cell, and a return at the end of each row (row-major order). Hence at the moment, the dump code...
9
9331
by: Mark G. | last post by:
Good afternoon. I have a "Fixed/2" format applied to a textbox on its property sheet. When I type data in the textbox, it is formatted correctly. But when I update the value programatically by setting the "value" property, the text does not format. I can get around this of course by using the VBA format() command. But is there any way I could update the value in code and have it display according to the format that is specified on the...
0
9374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9249
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
8244
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
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
4607
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...
1
3315
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
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.