输入一个浮点小数, 将其按4舍5入转换成整数, 并显示
来源:互联网 发布:下载民间小调软件 编辑:程序博客网 时间:2024/05/16 00:41
刷了这道题, 感觉只有自己上机查资料才能作出来, modf 头一次用, 原来还有这么个函数.
如果自己模拟modf, 只能用 float * 10 后,再mod 10来判断第一个小数位是否需要符合5入的条件.
用 modf 来做,逻辑清晰简洁.
// testvc6.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdio.h>#include <math.h>void fnTest();int float2int(float fIn);int main(int argc, char* argv[]){fnTest();getchar();return 0;}void fnTest(){/**输入一个浮点小数, 将其按4舍5入转换成整数, 并显示之.*/int iOut = 0;float fIn = 0.0f;printf("please input a float :");scanf("%f", &fIn);/** test case : fIn = 9.12, iOut = 9fIn = 9.54, iOut = 9*/// iOut = (int)fIn;// iOut = (int)(fIn + 0.50f);// iOut = (int)(9.60f); ///< 9.6f 强转成int, 也是9iOut = float2int(fIn); ///< 9.6f to 10 and -9.6f to -10printf("[%f] convert to [%d]\r\n", fIn, iOut);}int float2int(float fIn){double f_mod = .0f; ///< 小数部分double f_int = .0f; ///< 整数部分/** test casefIn = -9.4, returrn -9fIn = -9.6, returrn -10fIn = 9.4, returrn 9fIn = 9.6, returrn 10*/f_mod = modf(fIn, &f_int);if (fIn >= .0f){/// 正浮点数if (f_mod >= 0.5f){f_int += 1; ///< 9.6f to 10}}else{/// 负浮点数if (f_mod < -0.5f){f_int -= 1; ///< -9.6 to -10}}return (int)f_int;}
<2015-1019>
如果不用modf, 直接用 +0.5f 或 -0.5f 也可以将float 转成 int. 这样还简单一些.
不过modf 可以方便的分离float的整数部分和小数部分,在别的场合还是挺有用的.
// testvc6.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdio.h>#include <math.h>void fnTest();void clear_input_buffer();int float2int(float fIn);int main(int argc, char* argv[]){fnTest();printf("END, press any key to quit\n");getchar();return 0;}void fnTest(){/**输入一个浮点小数, 将其按4舍5入转换成整数, 并显示之.*/int iOut = 0;float fIn = 0.0f;printf("please input a float :");scanf("%f", &fIn);clear_input_buffer();/** test case : fIn = 9.12, iOut = 9fIn = 9.54, iOut = 9*/// iOut = (int)fIn;// iOut = (int)(fIn + 0.50f);// iOut = (int)(9.60f); ///< 9.6f 强转成int, 也是9iOut = float2int(fIn); ///< 9.6f to 10 and -9.6f to -10printf("[%f] convert to [%d]\r\n", fIn, iOut);}int float2int(float fIn){double f_int = .0f; ///< 整数部分/** test casefIn = -9.4, returrn -9fIn = -9.6, returrn -10fIn = 9.4, returrn 9fIn = 9.6, returrn 10*//// 正浮点数 和 负浮点数 需要加的0.5f 方向不同 f_int = fIn + ((fIn >= .0f) ? 0.50f : (- 0.50f));return (int)f_int;}void clear_input_buffer(){char ch = '\0';/// scanf 的回车 0x0a, 留在了buffer里面, 用getchar()还能读的到, /// clear input bufferdo{ch = getchar();} while ((ch != EOF) && (ch != '\n'));}
0 0
- 输入一个浮点小数, 将其按4舍5入转换成整数, 并显示
- 2.5 输入一个单精度浮点数,将其向零舍入到整数。说明:向零舍入的含义是,正数向下舍入,负数向上舍入。提示:可以使用强制类型转换来实现
- 将一个浮点数分成整数和小数部分,金额转换成大写
- 题目1194:八进制 输入一个整数,将其转换成八进制数输出
- java实现输入一个整数,将其转换成八进制数输出。
- 八进制 输入一个整数,将其转换成八进制数输出
- 输入一个整数字符串,将该字符串转换成整数并输出
- 3.7 编写一个程序,以字符形式输入某进制的数字,将其转换为十进制数值并显示出
- 将一个浮点数的整数部分和小数部分分别输出显示,java显示(1H)
- js将对象所有属性(都是数字,浮点小数),将其保留整数
- javascript将浮点数转换成整数
- 将字符串转换成浮点数整数
- 初入C语言!整数算法训练2:输入5个数(含负数、小数),将它们按由小到大的顺序排列起来。
- 第5.2节 函数getint接受自由格式的输入,并执行转换,将输入的字符分解成整数,且每次调用得到一个整数
- (c语言)输入一个整数,将其逆序输出
- JAVA 输入一个浮点,分别输出整数部分和小数部分
- 设计一个程序,输入3个整数,将其按从大到小的顺序输出
- 设计一个程序,输入3个整数,将其按从大到小的顺序输出
- 【IOS 开发学习总结-OC-54】★★ios开发UI 控件——UINavigationBar 与UINavigationController
- 不要只替换一个类
- 【c++程序】关于operator
- 双向栈的初始化,压栈,出栈
- 自定义类为什么要实现Serializable
- 输入一个浮点小数, 将其按4舍5入转换成整数, 并显示
- linux 安装环境 记录
- html验证码生成
- 存储过程和函数的区别
- 1037. 在霍格沃茨找零钱(20)
- sizeof 问题总结
- 关于Java的数据类型以及相互转换
- 【黑马训练营】IOS C语言通讯录
- Android Support v4、v7、v13 介绍