Rein Valdez -5

Hello Daniweb Forum People, I have this problem of getting the decimal of a number? How can I get it and copy to a variable?

ex.... 1234.59

I used a for loop in order to get the index value of 59, my problem was with the value? how can I transfer this index number to a variable?

WaltP 2,905

Not a clue what you are talking about.

Rein Valdez -5

Im having a problem of getting the values of this string for example

1234.56, I want to extract the 56 in order to have that value. I used a for loop? I am right or their other way how to that?

How can i passed on the value of that extracted to an int one. Thanks

Aia 1,977

strchr() can find where the period is, the rest is the decimal part.
strtol() can convert the decimal part into a long int.

Rein Valdez -5

thank you aia for the answer. Does the For Loop statement needed for this problem?

Rein Valdez -5

``````if((strchr(num,'.')!=0))
{
int cpr,com,hab,con;
cp = strchr(num,'.');
com = cp-num;
hab = strlen(num);
for (cpr=com+1; cpr<=hab;cpr++)
printf("%c",num[cpr]);
//x=num[cpr];
//printf("%c",x);
//con=atoi(num[cpr]);
printf("Print if got decimal");

}``````

This is my code snippet any help in order to get the decimal for example of this no. 1234.56? thanks.

Aia 1,977

strchr() returns a pointer to the first occurrence of the character you want, not to an int.

A call to strtol() will convert string number value representation to long int.

csurfer 422

If you just want to extract the index (part after the decimal pointer) you can use this method also (in case your index part is in integer bounds)
If you have scanned it as a floating point number:

1>Convert it into a STRING.
2>Then use standard function strtok() with the delimiter as "." and get the second token.
3>This is your index but in string format.
4>Convert it into an integer number.

If you have taken the input as string itself and want the index to be in string format only then disregard step 1 and 4.

rajenpandit -1

Sorry Aia actually i m not getting the concept of

cp = strchr(num,'.');
com = cp-num;

can u plz explain for me.........
thanx

nucleon 114

That's not Aia's code, and it seems to be incorrect. Here's a working example. It lacks proper error checking for strtol, though. See the link in Aia's post above for the scoop on strtol.

``````int main (void)
{
char *number = "1234.56";
char *decimal;
long int fraction = 0;

if ((decimal = strchr (number, '.')) != NULL)
fraction = strtol (decimal + 1, NULL, 10);

printf ("%ld", fraction);
}``````

Rein Valdez -5

``````startPos = strlen(MyInput);
do {
startPos = startPos - 3;
if (startPos < 0) {
numdigits = numdigits + startPos;
startPos = 0;
}
strncpy(threedigits, MyInput + startPos, numdigits);
threedigits[numdigits] = '\0';
printf("%s ",threedigits);``````

Another problem of mine, due to editing of the whole program, this code snippet chop the input number by 3 digits. ex. 123456789 will output 789 456 123. How could I get the value first of 789 in the variable threedigits? thanks.

Aia 1,977

>How could I get the value first of 789 in the variable threedigits?

Given:

``````char number[] = "123456789";
char *chopchop;
char threedigits[4] = {'\0'};``````

Do:

``````/* start at beginning of wanted digists  */
chopchop = &number[strlen(number) - 3];

/* copy wanted digits as a string */
strcpy(threedigits, chopchop);``````

include <string.h> for `strlen()` and `strcpy()`