How can we extract the decimal part of a floating point number and store the decimal part and the integer part into two separate integer variables?
Here is another way:
#include
int main()
{
char* inStr = "123.4567"; //the number we want to convert
char* endptr; //unused char ptr for strtod
char* loc = strchr(inStr, '.');
long mantissa = strtod(loc+1, endptr);
long whole = strtod(inStr, endptr);
printf("whole: %d \n", whole); //whole number portion
printf("mantissa: %d", mantissa); //decimal portion
}
http://codepad.org/jyHoBALU
Output:
whole: 123
mantissa: 4567