How to evaluate a float to first decimal place

This is a discussion on How to evaluate a float to first decimal place within the C Programming forums, part of the General Programming Boards category; Hi, id like to know how to evaluate a float to the first decimal place, i considered converting the float ...

1. How to evaluate a float to first decimal place

Hi, id like to know how to evaluate a float to the first decimal place, i considered
converting the float to a char and then evaluating it that way, however i think there
is probably a way of doing this that isn't as extravagant. I want to do this as i am
writing something that converts denary to binary. Any feedback would be
greatly appreciated.

2. So, you want the integer part, e.g out of 47.11, you want 47. Or do you want the integer part AND the first decimal place, e.g. 47.1?

Assuming also that the integral value is within the range of an integer, you could use an integer to get the integral part of a number. To get the integral part and an additional decimal, you can get the whole, then subtract that from the float, and multiply the result by ten - the integer part of this is the first decimal place.

--
Mats

4. Do you need to do math with the rounded values, or just display them? If you need to do math, use fmod as dwks suggests, or use a fixed point math library. If you just need to display a rounded result, you need to specify the precision of the number to the output stream.

5. If you just need to display a rounded result, you need to specify the precision of the number to the output stream.
For example:
Code:
`printf("%.1f", 3.14);  /* prints 3.1 */`

6. Thanks for the replies. Sorry I worded it slightly wrong, the first decimal place is needed, for example with 156.76, i'd need the 8. The integer part is irrelevant. If you are perplexed as to why the decimal place is needed, skim the first part of this http://www.btinternet.com/~a.morris7...uter-Maths.pdf

7. Originally Posted by redruby147
Thanks for the replies. Sorry I worded it slightly wrong, the first decimal place is needed, for example with 156.76, i'd need the 8. The integer part is irrelevant. If you are perplexed as to why the decimal place is needed, skim the first part of this http://www.btinternet.com/~a.morris7...uter-Maths.pdf
Nothing in that handout requires this sort of thing, but if you want to do it, more power to you.

8. Code:
```\$ cat ./fmodtest.c
#include <stdio.h>
#include <math.h>

int main() {
double x = 156.76;
double int_part;
int first = (int)(modf(x, &int_part) * 10 + 0.5);
printf("First decimal place of %f is %d\n", x, first);
return 0;
}
\$ ./fmodtest
First decimal place of 156.760000 is 8
\$```
Note that I've used modf(), not fmod()! I always get those two confused.

Also, if you're going to be dealing with negative numbers as well as positive ones, you'll have to use a more sophisticated rounding scheme than I have used above.

 I suppose I should have read the handout first. Are you trying to convert arbitrary real numbers into binary?

BTW, I think that "decimal" might be a more common term than "denary", though they both mean the same thing, base ten. Just in case someone comes up to you and starts talking about "decimal".
[/edit]

9. ....wow! *hugs dwks* Thanks, this is exactly what i was looking for .