解决方法
math.h库中包含一个名为modf的函数
有了这个功能,你可以做你想做的事.
有了这个功能,你可以做你想做的事.
例:
- #include <stdio.h>
- #include <math.h>
- double ftof ()
- {
- double floating = 3.40,fractional,integer;
- fractional = modf(floating,&integer);
- printf ("Floating: %g\nInteger: %g\nFractional: %g",floating,integer,fractional); // when using printf,there are no floats
- return fractional;
- }
输出:
- Floating: 3.40
- Integer: 3
- Fractional: 0.40
请注意,在大多数情况下使用double比使用float更好,尽管有两倍消耗浮动内存的两倍(4:8字节)因此增加了范围和准确性.如果您需要更精确的输出打印时浮动数字越大,您可以尝试使用printf()指数格式说明符%e而不是仅使用%的%g浮动小数的最短表示.