# how to read a digit of a floating point number?????

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-13-2007
spicy_centipede
how to read a digit of a floating point number?????
how do we read a particular digit/digits of a floating point number???

ive jus started out with C....hardly know anything and i was presented with this problem....

im supposed to write a program which accepts a floating point number from the user....and displays the rightmost digit of the integral part....(the digit at ones place)..

next..it asked me to modify the program such that two od the rightmost digits of the integral part of the number were displayed...

ill be very thankful for any help...
• 07-13-2007
mike_g
I'd convert it to a string, read it until reaching a dot or null char then get the character before it.
• 07-13-2007
MacGyver
Few ways of doing it:

1. I think you can call floor() on the float, and then cast it to an int. This should take something like 34.54 and convert it to 34.
2. Get the remainder of the int divided by 10 (Hint: % operator) 34 / 10 = 3, R = 4
3. Print.

1. Read in float as a string with fgets().
2. Find the '.' char.
3. If it exists, print the char before it.
4. If it doesn't exist, print the last char of the string.
• 07-13-2007
mike_g
Quote:

1. I think you can call floor() on the float, and then cast it to an int. This should take something like 34.54 and convert it to 34.
Would copying the float to an int not automatically floor it? Thats what I have been doing (i think).
• 07-13-2007
MacGyver
I think you're right, but for whatever reason I was under the impression the floor() was needed. I don't remember what prompted me to think that.
• 07-13-2007
brewbuck
Quote:

Originally Posted by mike_g
Would copying the float to an int not automatically floor it? Thats what I have been doing (i think).

Consider negative numbers.

EDIT: Also, modf() is what you want.
• 07-13-2007
abachler
The string method using sprintf() is probably the best. just read until you see the period or reach teh end of the string

Code:

``` void func(double input){     char temp[256];     int index;       sprintf((char*)temp , "&#37;f" , input);     index =0;     while((temp[index] != 0) && (index<256) && (temp[index] != '.')) index++;     index--;       // index now points at the ones place         return;     }```
• 07-13-2007
dwks
You could make your job easier with &#37;.0f:
Code:

```double get_first_digit(double number) {     char buffer[BUFSIZ];     sprintf(buffer, "%.0f", number);     if(*buffer) {         return buffer[strlen(buffer) - 1] - '0';     }     return 0; }```
But anyway, using a string is a bad idea in my opinion. You use lots of extra memory, and sprintf()'s probably pretty slow too. The mathematical way isn't much harder, if any.

Quote:

I think you're right, but for whatever reason I was under the impression the floor() was needed. I don't remember what prompted me to think that.
Casting a floating point number to an int would indeed have the same effect as flooring it. But using floor() and modf() instead of a cast to an integral type is a much better idea, because a floating point number can easily store values that cannot be represented in any integral type. If you casted one of those values to (int), you'd get an integer overflow.

So floor() exists so that you can round a number without risk of overflow.

Anyway, you could also use something like this:
Code:

```double get_first_digit(double number) {     return floor(floor(number/10)*10 - number); }```
That's probably a bad way to do it . . . perhaps something like this, then.
Code:

```double get_first_digit(double number) {     return floor(fmod(number, 10)); }```
To get the first decimal digit, of course.
• 07-13-2007
brewbuck
Quote:

Originally Posted by dwks
Casting a floating point number to an int would indeed have the same effect as flooring it.

Again though, not always. Consider a negative number. On my system, (int)(-3.5) == -3, not -4. A floor() operation would have taken that value to -4.0.
• 07-13-2007
MacGyver
Quote:

Originally Posted by brewbuck
Again though, not always. Consider a negative number. On my system, (int)(-3.5) == -3, not -4. A floor() operation would have taken that value to -4.0.

Floor()ing the float under that condition is not what the OP wants I take it. Straight casting might be better if he chooses to do conversions to an int.

The string method still sounds good. lol...
• 07-13-2007
brewbuck
Quote:

Originally Posted by MacGyver
Floor()ing the float under that condition is not what the OP wants I take it. Straight casting might be better if he chooses to do conversions to an int.

Code:

```int last_digit_of_integral_part(float v) {     if(v < 0.0) v = -v;     return (int)((v * 0.1 - (int)(v * 0.1)) * 10.0); }```
• 07-13-2007
dwks
I like
Code:

```double get_first_digit(double number) {     if(number < 0) {         return ceil(fmod(number, 10));     }     else {         return floor(fmod(number, 10));     } }```
Or if the digit alone was required, without a sign:
Code:

`floor(fmod(fabs(number), 10));`
 The C99 round() would work too, replacing floor() and ceil().
Code:

`round(fmod(number, 10));`
[/edit]
• 07-13-2007
iMalc
Using floor explicitly also ensures correct operation if the /QIfist compiler option is turned on. (Which I currently do for my software 3D engine)
• 07-13-2007
linuxdude
Code:

`printf("%1.0f\n",num);`
this has a width of 1 and since no decimals all the width will be in the ones place right?
similarly
Code:

`printf("%2.0f\n",num);`
• 07-14-2007
MacGyver
Code:

```#include <stdio.h> int main(void) {         float fNum = 123.456;                 printf("fNum = &#37;1.0f\n",fNum);                 return 0; }```
Output:

Code:

`fNum = 123`
In other words, no. That's wrong.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last