当然,C++ 11提供各类型的std::round来四舍五入,但是没有一个能直接支持保留小数点位数的四舍五入方案。

所以需要通过setprecision来实现:

 #include <iomanip>
#include <iostream>
std::cout << std::fixed << setprecision() << 5.324356;

以上就是保留5位小数并四舍五入,如果把std::fixed去掉,那么就是保留5位有效位数并四舍五入。如果觉得标准输出流处理不方便,可以用封装stringstream类来实现字符串的转换的四舍五入函数:

 #include <iomanip>
#include <sstream>
#include <iostream> static std::string roundAny(double r,int precision){
std::stringstream buffer;
buffer << std::fixed << setprecision(precision) << r;
return buffer.str();
} int main()
{
std::cout << roundAny(45.65658866,) << std::endl; // C++11
std::cout << roundAny(21.6463,) << std::endl; // C++11
return ;
}

C++ 11保留小数点的四舍五入方案的更多相关文章

  1. C# Double保留小数点后面位数

    C# Double保留小数点后面位数 有的时候我们要对一些数据进行百分比的操作.一般的数据我们只要用 .ToString("P")就可以得到小数点后2位的百分比.而且是自动 加上% ...

  2. JAVA保留小数点位数

    /** * java 如何保留指定位数的小数 * @author Administrator * */ public class Test04 { public static void main(St ...

  3. java保留小数点的几个方法

    方法一: String类自带的方法 String.format("%.2f", 1.2548); "%.2f"其中的数字决定保留几位方法二: 格式化的方法 pr ...

  4. Flutter实战视频-移动电商-11.首页_屏幕适配方案讲解

    11.首页_屏幕适配方案讲解 国人写的屏幕适配插件: https://github.com/OpenFlutter/flutter_screenutil 最新版本是0.5.1 在pubspec.yam ...

  5. (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案二

    http://blog.csdn.net/yerenyuan_pku/article/details/52894958 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...

  6. (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一

    http://blog.csdn.net/yerenyuan_pku/article/details/52888808 前面我们已经集成了Spring4.2.5+Hibernate4.3.11这两个框 ...

  7. 【iOS】stringWithFormat 保留小数点位数 float double

    以前就见过,如下: text = [NSString stringWithFormat:@"%.1f", percentageCompleted]; 但一直没在意.刚一时好奇,查了 ...

  8. JS保留小数点(四舍五入、四舍六入)实例

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  9. java四舍五入BigDecimal和js保留小数点两位

    java四舍五入BigDecimal保留两位小数的实现方法: // 四舍五入保留两位小数System.out.println("四舍五入取整:(3.856)="      + ne ...

随机推荐

  1. Java hashCode() 和 equals()的若干问题解答<转载自skywang12345>

    第1部分 equals() 的作用equals()的作用是用来判断两个对象是否相等.equals()定义在JDK的Object类中.通过判断两个对象的地址是否相等(即,是否是同一个对象)来区分它们是否 ...

  2. 华清远见Linux设备驱动(每章小结)

    1.  linux设备驱动是以内核模块的方式而存在的,在具体的驱动开发中将驱动编译为模块具有很到的工程意义.因为如果将正在开发中的驱动编译如内核,而开发过程中会不断修改驱动代码,则需要不断的编译和重启 ...

  3. [leetcode]Word Break II @ Python

    原题地址:https://oj.leetcode.com/problems/word-break-ii/ 题意: Given a string s and a dictionary of words  ...

  4. Android -- DiskLruCache

    DiskLruCache 创建一个磁盘缓存对象: public static DiskLruCache open(File directory, int appVersion, int valueCo ...

  5. grunt-cmd-transport提取deps[]的BUG

    该BUG已经在GitHub上提了issue,详见:#56 文件 // employee/static/adder.js define(function (require, exports) { exp ...

  6. lstm(一) 演化之路

    递归神经网络引入了时序的反馈机制,在语音.音乐等时序信号的分析上有重要的意义. Hochreiter(应该是Schmidhuber的弟子)在1991年分析了bptt带来的梯度爆炸和消失问题,给学习算法 ...

  7. SQL-order by两个字段同时排序

    ORDER BY 后可加2个字段,用英文逗号隔开. --f1用升序, f2降序,sql该这样写 ORDER BY f1, f2 DESC --也可以这样写,更清楚: ORDER BY f1 ASC, ...

  8. 基于浏览器的开源“管理+开发”工具,Pivotal MySQL*Web正式上线!

    基于浏览器的开源“管理+开发”工具,Pivotal MySQL*Web正式上线! https://www.sohu.com/a/168292858_747818 https://github.com/ ...

  9. How could I create a custom windows message?

    [问题] Our project is running on Windows CE 6.0 and is written in C++ . We have some problems with the ...

  10. Logcat用法初探

    首先定位到adb所在的目录 将手机连接上电脑. 在命令行运行: adb devices 这个命令可以列出所有连上的移动设备. 在命令行运行: adb logcat 可以显示日志.   以下是例子截图: ...