473,320 Members | 1,951 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,320 software developers and data experts.

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

1
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 3723
arnaudk
424 256MB
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
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
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 <=...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
23
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...
12
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"...
4
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
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
11
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,...
9
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.