刚刚做了一个杭电的题目,上面有个要求是输出时要求保留小数点后几位数字后然后输出。既然又看到了这个熟悉的字眼和要求,那就在这里稍微写些东西算是记载一下吧,也算是一个小总结,不过这里总结的只是目前我所想到的,后续还有待补充。】

1. C++中格式控制
      在C++中,说到保留小数点后几位有效数字,就会想起setprecision,马上去cplusplus上查了下有关setprecision的资料,看了后明白了,懒得逐字翻译,直接贴在下面了:
Set decimal precision

Sets the decimal precision to be used by output operations.

Behaves as if a call to the stream's member ios_base::precision with n as argument was made.

The decimal precision determines the maximum number of digits to be written on insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is neither fixed nor scientific):

  • On the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision. (此处稍微解释下:在默认情况下,setprecision(n)中的参数n表示的是小数点前后所有的有效数字(从第一个不是0的数字开始计数)位数,并且如果数字本身所有的小数位数比要求保留的位数少的话,不再后面加零凑齐所要求的位数,而在下面所说的fixed和scientific情况下setprecision(n)中的参数n表示的才是小数点后的有效数字位数,并且如果数字本身所有的小数位数比要求保留的位数少的话,在后面加零凑齐,这几点在应用中要注意。)
  • In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The number of digits before the decimal point does not matter in this case.
  • This manipulator is declared in header <iomanip>, along with the other parameterized manipulators: resetiosflags, setiosflags, setbase, setfill and setw. This header file declares the implementation-specific smanip type, plus any additional operator overload function needed to allow these manipulators to be inserted and extracted to/from streams with their parameters.

测试代码如下:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    //以下所有保留小数的情形都考虑了四舍五入的情况
    double t = 1.125;
    //以下语句中的2包括小数点前的数字1
    cout<<setprecision(2)<<t<<endl;
    //虽然5>3,但是输出结构不用0来凑齐
    cout<<setprecision(5)<<t<<endl;
    //注意:一旦在下一条语句中加入了格式控制fixed,那么在后面的cout语句中
    //除非显示的改变格式控制,否则就默认按照前面规定的fixed格式输出
    cout<<fixed<<setprecision(2)<<t<<endl;
    //以下两条语句的输出格式是一样的
    cout<<setprecision(5)<<t<<endl;
    cout<<fixed<<setprecision(5)<<t<<endl;
    //显示修改了输出控制方式,以下两条语句的输出格式也是一样的
    cout<<scientific<<setprecision(2)<<t<<endl;
    cout<<setprecision(2)<<t<<endl;
    return 0;
}

输出如下:

注意:以上是默认四舍五入的.

2. 直接用C语言输出语句printf就可以搞定

测试代码如下:

#include <cstdio>
using namespace std;
int main()
{
    double t = 1.23456;
    printf("%0.3f\n",t);
    printf("%0.4f\n",t);
    printf("%0.5f\n",t);
    printf("%0.6f\n",t);
    return 0;
}

输出结果为:

从第二个输出我们也可以看出,printf的格式控制默认也是四舍五入的。
3. 自己想办法模拟实现。
写了个代码如下:

#include <iostream>
using namespace std;
double n_hundred(int n)
{
    double sum = 1.0;
    for(int i = 1;i <= n;++i)
        sum = sum *10;
    return sum;
}
double process(double t,int n)
{
    //如果要求保留t小数点后n位数字的话
    int ival = (int)(t * n_hundred(n));
    //看小数点后第n+1位数字是否大于5来进行四舍五入
    int temp = (int)(t * n_hundred(n+1))%10;
    if(temp >= 5)
        ival = ival + 1;
    double dval = ival/n_hundred(n);
    return dval;
}
int main()
{
    double t = 1.23456;
    cout<<process(t,3)<<endl;
    cout<<process(t,4)<<endl;
    cout<<process(t,8)<<endl;
    return 0;
}

输出结果如下:

处理基本的四舍五入和保留小数点后几位有效数字是可以的,但是有一个问题,就是如果要求保留的小数位数比现有的小数位数多的话,后面没法用0来补齐,就像上面程序的第三个输出那样的。
好了,先写到这里了,以后有新想法了再来继续写。

转载于:https://www.cnblogs.com/krisdy/archive/2009/04/17/1438402.html

关于保留小数点后几位数字之我见相关推荐

  1. 关于保留小数点后几位数字“

    原文地址:http://www.cnblogs.com/krisdy/archive/2009/04/17/1438402.html 刚刚做了一个杭电的题目,上面有个要求是输出时要求保留小数点后几位数 ...

  2. Java中如何保留小数点后几位数字

    保留小数点后几位数字 对于一些Java的初学者(博主也只算平民级别,以下是我的见解,可能有小错误,有错莫怪),如何保留一个double型小数点后固定的位数很是让人苦恼,因为我刚学的时候不知道如何保留小 ...

  3. c语言 float 保留小数点后两位数字

    挺实用的小技巧. float sp = 36.51647; sp=( (float)( (int)( (sp+0.005)*100 ) ) )/100;

  4. 请编写一个个人所税计算器,用户输入应发工资薪金所得、五险一金金额和个税免征额,输出应缴税款和实发工资,结果保留小数点后两位。当输入数字小于0或等于0时,输出“error”。‪

    题目 假设个人所得税税率表如下:‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬‬ ...

  5. thinkphp:数值(保留小数点后N位,四舍五入,左侧补零,格式化货币,取整,生成随机数,数字与字母进行转换)

    一.保留小数点后N位/类似四舍五入(以保留小数点后三位为准) number_format()函数:第一个参数为要格式化的数字,第二个参数为保留的小数位数 方法一: public function te ...

  6. VUE 框架添加全局公共方法 , 保留小数点后两位

    在main.js 里面给Vue对象添加方法. 来一个示例代码: import Vue from 'vue' import App from './App'Vue.prototype.num_to_st ...

  7. java 计算26个字母在一段文本中出现的频率(保留小数点后4位)

    public class FrequencyCalculator {public static void main(String[] args){//定义需要计算字母出现频率的文本String tex ...

  8. 使用js,对数值保留小数点后两位的处理(两种情况)

    Html部分: <div class="text primary-text"><span>合计:</span><span class=&q ...

  9. 金额保留小数点后两位方法

    vue金额格式化的方法 封装全局js文件,并在main.js中引用 //global.js export default {install(Vue) {Vue.prototype.$moneyForm ...

  10. java保留小数点后两位(小数点保留两位方法)

    java.怎样简便的保留小数点后两位. 有两种情况: 1.只要输出结果的时候可以用以下方法: double x1 = 0.026; System.out.println(String.format(& ...

最新文章

  1. simulink中使用memory模块实现变量的累加和(离散积分器)
  2. php 获取文件后缀_php获取文件后缀的9种方法
  3. nginx大量TIME_WAIT的解决办法 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'...
  4. elasticsearch之Recovery
  5. TVS 击穿电压和钳位电压的区别
  6. Tensorflow安装笔记
  7. WinXP中鲜为人知的28项隐藏功能
  8. XML——解析XML文档
  9. 非Java专家的APM:什么泄漏?
  10. java jaxb注解xmlnull_java – 将空值表示为xml jaxb中的空元素
  11. 动态规划求解装箱问题(洛谷P1049题题解,Java语言描述)
  12. 21天Jmeter打卡Day10线程用户之setUp和tearDown
  13. 朴素贝叶斯算法+模型的评价-查准率、召回率、F1-score及混淆矩阵(code实现)
  14. leetcode 7. 反转整数(python3)
  15. EXCEL滚动表格时保持第一行标题不动
  16. JS二维数组排序组合
  17. 二维数组矩阵常用实现方法
  18. 68超标量流水线的基本概念
  19. 获取海康摄像机/录像机rtsp视频流地址格式
  20. HDU - 1242

热门文章

  1. 商品包含资源和劳动两部分内容
  2. 《一秒学会C++》异步回调函数(C++11)
  3. windows 核心编程下的内存映射文件
  4. win10卸载电脑管家就蓝屏_Win10系统运行腾讯软件出现蓝屏TesSafe.sys 解决方案
  5. python中object类的源码在哪里-[Python之路] object类中的特殊方法
  6. VMware的配置问题集锦(更新中......)
  7. 实力封装:Unity打包AssetBundle(四)
  8. 精通Hyperledger之Hyperledger composer查询语言(17)
  9. 读Java面向对象编程(孙卫琴)
  10. EasyIcon:免费图标搜索和下载平台