# 从C中的浮点数中提取小数部分

double integral; double fractional = modf(some_double, &integral);

int main() { double num = 23.345; int intpart = (int)num; double decpart = num - intpart; printf("Num = %f, intpart = %d, decpart = %f\n", num, intpart, decpart); }

Num = 23.345000, intpart = 23, decpart = 0.345000

#define N_DECIMAL_POINTS_PRECISION (1000) // n = 3. Three decimal points. float f = 123.456; int integerPart = (int)f; int decimalPart = ((int)(f*N_DECIMAL_POINTS_PRECISION)%N_DECIMAL_POINTS_PRECISION);

#include <stdio.h> #include <stdlib.h> int main(void) { char s_value[60], s_integral[60], s_fractional[60]; int i, found = 0, count = 1, integral, fractional; scanf("%s", s_value); for (i = 0; s_value[i] != '\0'; i++) { if (!found) { if (s_value[i] == '.') { found = 1; s_integral[i] = '\0'; continue; } s_integral[i] = s_value[i]; count++; } else s_fractional[i - count] = s_value[i]; } s_fractional[i - count] = '\0'; integral = atoi(s_integral); fractional = atoi(s_fractional); printf("value = %s, integral = %d, fractional = %d\n", s_value, integral, fractional); return 0; }

void double2Ints(double f, int p, int *i, int *d) { // f = float, p=decimal precision, i=integer, d=decimal int li; int prec=1; for(int x=p;x>0;x--) { prec*=10; }; // same as power(10,p) li = (int) f; // get integer part *d = (int) ((f-li)*prec); // get decimal part *i = li; } void test() { double df = 3.14159265; int i,d; for(int p=2;p<9;p++) { double2Ints(df, p, &i,&d); printf("d2i (%d) %f = %d.%d\r\n",p, df,i,d); } }
void double2Ints(double f, int p, int *i, int *d) { // f = float, p=decimal precision, i=integer, d=decimal int li; int prec=1; for(int x=p;x>0;x--) { prec*=10; }; // same as power(10,p) li = (int) f; // get integer part *d = (int) ((f-li)*prec); // get decimal part *i = li; } void test() { double df = 3.14159265; int i,d; for(int p=2;p<9;p++) { double2Ints(df, p, &i,&d); printf("d2i (%d) %f = %d.%d\r\n",p, df,i,d); } }

#include <stdlib.h> 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 }

whole: 123 mantissa: 4567

int leftSideOfDecimalPoint = (int) initialFloatValue; // The cast from float to int keeps only the integer part int temp = (int) initialFloatValue * 10; int rightSideOfDecimalPoint = temp % 10;

• 通过只将最初的浮点数值转换为一个int值，就可以丢弃小数部分，只保留整数部分。
• 通过将初始浮点值乘以10得到278.0的结果，然后通过将此结果转换为int，给出值278
• 如果用10除278，得到27.8，其余8是小数点右边的值。 因此使用模数。

#include <math.h> void GetFloattoInt (double fnum, long precision, long *pe, long *pd) { long pe_sign; long intpart; float decpart; if(fnum>=0) { pe_sign=1; } else { pe_sign=-1; } intpart=(long)fnum; decpart=fnum-intpart; *pe=intpart; *pd=(((long)(decpart*pe_sign*pow(10,precision)))%(long)pow(10,precision)); }
cout<<"enter a decimal number\n"; cin>>str; for(i=0;i<str.size();i++) { if(str[i]=='.') break; } for(j=i+1;j<str.size();j++) { cout<<str[j]; }