0

Been trying to get the outputs to give me the number without rounding, not sure what I'm missing

float NumberOfTrees1; for (NumberOfTrees1 = 1000.0; NumberOfTrees1 >= 50; NumberOfYears++) { NumberOfTrees1 = NumberOfTrees1 - (NumberOfTrees1 * 0.13); printf("Number of Remaining Trees: %0.1f\n",NumberOfTrees1); } 

My Output:

Number of Remaining Trees: 61.7 Number of Remaining Trees: 53.7 Number of Remaining Trees: 46.7 

Required Output:

Number of Remaining Trees: 61 Number of Remaining Trees: 53 Number of Remaining Trees: 46 

I understand that the %0.1f is what gives me the .7 but when I use %0.0f it rounds up my numbers which I don't want. Been switching things around from using int, double, long float etc to no avail.

Any help would be great and thank you in advance!!!

6
  • By subtracting n*0.13 you're likely to have decimals. What don't you want: 46.7 rounded up to 47 or down to 46, or both? Commented Mar 20, 2021 at 5:42
  • 1
    You may be interested in the floor() function. Commented Mar 20, 2021 at 5:44
  • 1
    Read the floating-point-gui.de Commented Mar 20, 2021 at 5:58
  • 1
    Printf rounds to nearest, not round up Commented Mar 20, 2021 at 6:26
  • 1
    double (and float) itself is rounded naturally (0.1 + 0.2 results in a rounded 0.3)... but just use "%a" to print all the bits (in hexadecimal): printf("0.3 rounded is %a\n", 0.1 + 0.2); Commented Mar 20, 2021 at 9:06

2 Answers 2

1

Antonin GAVRELs answer should print the desired output, but if you don't want to cast the floating point numbers into integers, you could use the double floor (double x) function from <math.h>.

#include <math.h> #include <stdio.h> int main(void) { double x = 53.7; double floored = floor(x); printf("⌊%0.1lf⌋ = %0.1lf\n", x, floored); return 0; } 

The program can be compiled with -lm and prints the following when executed:

⌊53.7⌋ = 53.0 
Sign up to request clarification or add additional context in comments.

Comments

0

You can cast into int in order to truncate your number:

int main(void) { double d = 53.7; float f = 46.7f; int a = (int)d; int b = (int)f; printf("%d %d\n", a, b); } 

output:

53 46 

Or, as suggested by @user3386109, you may use floor(), but you will need to also include <math.h> header and compile with -lm

9 Comments

Is there any other way other than casting it into "int"? I'm taking a class and we haven't covered that as well as using floor(). Thank you though
you can use floor function from libc which truncates the number
Why do you use the suffix f on the float? It's only needed on the double.
Casting to int won't work if the float value is outside int's range. The only safe way is floor
UPVOTE for an answer!!!!! Quick and dirty way of doing this, my only suggestion is, put the NOTE: If you know that your value is larger than MAX_INT cast it to long or long long instead. What if you do not have math library?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.