# How to take only two decimals of float without rounding?

This is a discussion on How to take only two decimals of float without rounding? within the C Programming forums, part of the General Programming Boards category; Hi I am brand new to C and am trying to understand how the float and double variables work. What ...

1. ## How to take only two decimals of float without rounding?

Hi I am brand new to C and am trying to understand how the float and double variables work. What I am trying to do is initialize a variable to say, x = 6.789. And then from there just print out a basic statement such as:

printf("The furniture cost \$ %.2f", x);

However whenever I do this it always rounds the variable to \$6.79. Is there a way to make it say \$6.78? I just need help understanding how it works, if anyone could just get me started that would be great.

Thanks Cody

IGNORE THREAD: It was a very basic understanding assignment I was working on but the teacher has announced a there was a typo which solves my problem, I would delete the thread but im not sure how, sorry.

2. Someone correct me if I'm wrong, but this line
Code:
`%.2f`
Will "create" a float with 2 decimals from the float you have (".2f" seems quite obvious). And the correct mathematical way to do this is to round it.

Being a cnewbie I don't know if there exists a easy way to specify x decimals without rounding. My first impulse would be to treat the float variable as a string and just cut away everything after .xx (all decimals after the second) manually.

3. Look up the floor() function in IIRC math.h; might not do what you want but it or round() with division/multiplication should.

Homemade code without math.h, not tested

printf("The furniture cost \$ %.2f", (int)(x*100)/100.0);

Homemade code with math.h, not tested
printf("The furniture cost \$ %.2f", floor(x*100)/100.0);

Tim S.

4. Originally Posted by cnewbie1
And the correct mathematical way to do this is to round it.

Being a cnewbie I don't know if there exists a easy way to specify x decimals without rounding. My first impulse would be to treat the float variable as a string and just cut away everything after .xx (all decimals after the second) manually.
Rounding is certainly the behavior that most people would expect, but I wouldn't say it's the correct way. Truncation is perfectly valid and correct, even if it's less used than rounding. The IEEE 754 floating point standard, which is what most systems conform to, specifies 5 rounding methods, including the truncation that the OP was interested in. Whether the OP has a fully conforming implementation and how s/he will set the rounding method, I can't say.

Originally Posted by stahta01
Look up the floor() function in IIRC math.h; might not do what you want but it or round() with division/multiplication should.
Try trunc() with some multiplication/division. It is also in math.h and avoids the problems you will encounter with floor and negative arguments.

5. anduril: Yup, you're right of course. Bad choice of words on my part.