Ede*_*nia 23
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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
Floating: 3.40
Integer: 3
Fractional: 0.40
Run Code Online (Sandbox Code Playgroud)
请注意,double
在大多数情况下使用比使用更好float
,尽管它double
消耗了两倍float
(4:8字节)的内存,因此增加了范围和准确性.此外,如果您在打印时需要更大的浮动数字更精确的输出,您可以尝试printf()
指数格式说明符,%e
而不是%g
仅使用浮动小数的最短表示.