如何保证输出float类型的整数位,舍弃所有小数位值并填充数字0?

uison 2018-09-22 08:30:15
如题!新手求帮助。我定义了一个float a,但是输出时想舍弃小数位的值填充相应的数字0。
比如输入128显示128.0,输入128.6则显示128.0
新手刚开始看,哈哈哈。
...全文
720 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
辶井 2018-10-11
  • 打赏
  • 举报
回复
把float a改成int a,在输出的时候再强转float
赵4老师 2018-09-25
  • 打赏
  • 举报
回复
引用 4 楼 zangfong 的回复:
偷懒一点,如果按照楼主的意思,直接抹去小数点后的数字而填充0的话,直接
printf("%d.0\n",(int)a);

头文件都省了

别忘了float能表示的数范围。
zangfong 2018-09-25
  • 打赏
  • 举报
回复
恩,赵老师说的对,不然就多了一个bug
zangfong 2018-09-22
  • 打赏
  • 举报
回复
偷懒一点,如果按照楼主的意思,直接抹去小数点后的数字而填充0的话,直接
printf("%d.0\n",(int)a);
头文件都省了
赵4老师 2018-09-22
  • 打赏
  • 举报
回复
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
赵4老师 2018-09-22
  • 打赏
  • 举报
回复
floor
Calculates the floor of a value.

double floor( double x );

Function Required Header Compatibility
floor <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

The floor function returns a floating-point value representing the largest integer that is less than or equal to x.There is no error return.

Parameter

x

Floating-point value

Example

/* FLOOR.C: This example displays the largest integers
* less than or equal to the floating-point values 2.8
* and -2.8. It then shows the smallest integers greater
* than or equal to 2.8 and -2.8.
*/

#include <math.h>
#include <stdio.h>

void main( void )
{
double y;

y = floor( 2.8 );
printf( "The floor of 2.8 is %f\n", y );
y = floor( -2.8 );
printf( "The floor of -2.8 is %f\n", y );

y = ceil( 2.8 );
printf( "The ceil of 2.8 is %f\n", y );
y = ceil( -2.8 );
printf( "The ceil of -2.8 is %f\n", y );
}


Output

The floor of 2.8 is 2.000000
The floor of -2.8 is -3.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000


Floating-Point Support Routines

See Also ceil, fmod
没法下载,到这里折腾一把试试。 本文由abc2253130贡献 doc文档可能在WAP端浏览体验不佳。建议您优先选择TXT,或下载源文件到本机查看。 C#(WINFORM)学习 一、 C#基础 基础 类型和变量 类型和变量 类型 C# 支持两种类型:“类型”和“引用类型”。类型包括简单类型(如 char、int 和 float 等)、枚举类型和结构类型。引用类型包括类 (Class)类 型、接口类型、委托类型和数组类型。 变量的类型声明 变量的类型声明 每个变量必须预先声明其类型。如 int a; int b = 100; float j = 4.5; string s1; 用 object 可以表示所有的类型。 预定义类型 下表列出了预定义类型,并说明如何使用。 类型 object 说明 所有其他类型的最终 基类型 字符串类型; 字符串是 Unicode 字符序列 8 有符号整型 16 有符号整型 32 有符号整型 64 有符号整型 示例 object o = null; 范围 string sbyte short int long string s = "hello"; sbyte val = 12; short val = 12; int val = 12; long val1 = 12; -128 到 127 -32,768 到 32,767 -2,147,483,648 2,147,483,647 -9,223,372,036,854,775,808 到 第1页 C#(WINFORM)学习 long val2 = 34L; 到 9,223,372,036,854,775,807 byte ushort 8 无符号整型 16 无符号整型 byte val1 = 12; ushort val1 = 12; uint val1 = 12; uint 32 无符号整型 uint val2 = 34U; ulong val1 = 12; ulong val2 = 34U; ulong 64 无符号整型 ulong val3 = 56L; ulong val4 = 78UL; float 单精度浮点型 float val = 1.23F;7 double val1 = 1.23; double 双精度浮点型 double val2 = ±5.0 × 10?324 ±1.7 × 10 308 0 到 255 0 到 65,535 0 到 4,294,967,295 0 到 18,446,744,073,709,551,615 ±1.5 × 10?45 ±3.4 × 10 38 到 到 4.56D;15-16 布尔型;bool 或为 真或为假 字符类型;char 是 一个 Unicode 字符 精确的小数类型, 具有 28 个有效数字 bool val1 = true; bool val2 = false; char val = 'h'; decimal val = bool char decimal DateTime ±1.0 × 10?28 ±7.9 × 10 28 到 1.23M;28-29 变量转换 简单转换: float f = 100.1234f; 可以用括号转换: short s = (short)f 也可以利用 Convert 方法来转换: string s1; s1=Convert.ToString(a); MessageBox.Show(s1); 常用 Convert 方法有: 第2页 C#(WINFORM)学习 C# Convert.ToBoolean Convert.ToByte Convert.ToChar Convert.ToDateTime Convert.ToDecimal Convert.ToDouble Convert.ToInt16 Convert.ToInt32 Convert.ToInt64 Convert.ToSByte Convert.ToSingle Convert.ToString Convert.ToUInt16 Convert.ToUInt32 Convert.ToUInt64 备注 Math 类 常用科学计算方法: C# Math.Abs Math.Sqrt Math.Ro

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧