Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing - please help

Status
Not open for further replies.

iamjd

Technical User
Feb 12, 2005
37
US
I have float number, 214.36, I want to take the second integral number which is "1".

What method should I use to accomplish this? I have looked for things on parsing but can't find anything?
 
One way:
Code:
#include <stdio.h>

int main ( void )
{
   double d = 214.36;
   int i = ((int)d / 10 ) % 10;
   printf ( "d = %g, i = %d\n", d, i );
   return 0;
}

/* my output
d = 214.36, i = 1
*/
 
Code:
float target = 214.36;
char change[25];

                  snprintf(change,25,"%f",target);
                  printf("%d\n",change[1]);
 
Yes, your result is right.
int i = ((int)d / 10 ) % 10
First, d was converted integer type:214, then result is 21.4. Finally 21.4 % 10, 21.4 has coverted "21", so "21 % 10" equal to "1".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top