请教一个C++分float的问题

C++语言 码拜 8年前 (2016-04-13) 1003次浏览
将一个float类型的数据分开成整数部分和小数部分,将结果返回主函数输出
问一下该怎么做啊
解决方案

10

记不得哪位C++大神在哪本学习C++的书的前言里面说过
“用C语言1000行源码能完成的工作千万不要用C++重写!”

30

modf
Splits a floating-point value into fractional and integer parts.
double modf( double x, double *intptr );
Routine Required Header Compatibility
modf <math.h> ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
This function returns the signed fractional portion of x. There is no error return.
Parameters
x
Floating-point value
intptr
Pointer to stored integer portion
Remarks
The modf function breaks down the floating-point value x into fractional and integer parts, each of which has the same sign as x. The signed fractional portion of x is returned. The integer portion is stored as a floating-point value at intptr.
Example
/* MODF.C */
#include <math.h>
#include <stdio.h>
void main( void )
{
double x, y, n;
x = -14.87654321;      /* Divide x into its fractional */
y = modf( x, &n );     /* and integer parts            */
printf( “For %f, the fraction is %f and the integer is %.f\n”,
x, y, n );
}
Output
For -14.876543, the fraction is -0.876543 and the integer is -14
Floating-Point Support Routines, Long Double
See Also   frexp, ldexp

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明请教一个C++分float的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)