【1】C++数字任意小数位显示

it2022-05-08  8

【1】任意位数显示

#include "pch.h" #include <iostream> #include <iomanip> using namespace std; int main() { double a = 7; double b = 3; double c = a / b; double d = 7 / 3; //float和double默认情况下显示六位有效数字 //float和double默认情况下显示六位有效数字 //float和double默认情况下显示六位有效数字 /****************************************************************************************************************/ //因为double类型有六个位数setprecision(0)到setprecision(6)显示的效果一样 cout << "double" << endl; cout << "double_distance5=" << setw(5) << c << endl;//2.33333 默认显示六位小数 cout << "double_distance1=" << setw(5) << setprecision(0) << c << endl; //2.33333 显示效果setprecision(0) << c =c cout << "double_distance2=" << setw(5) << setprecision(6) << c << endl; //2.33333 默认显示六位小数 cout << "double_distance3=" << setw(5) << setprecision(8) << c << endl; //2.3333333 保留八位小数 cout << "double_distance4=" << setw(5) << setprecision(2) << c << endl; //2.3 保留一位小数 cout << "double_distance4=" << setw(5) << setprecision(3) << c << endl; //2.33 保留两位小数 cout << "double_distance5=" << setw(5) << c << endl;//2.33 原因是此行代码直接读取上一行代码的值 /****************************************************************************************************************/ /****************************************************************************************************************/ //int是整形所以显示效果一样 cout << "int" << endl; int e = a / b; cout << "int_distance1=" << setw(5) << setprecision(1) << e << endl; //2 cout << "int_distance2=" << setw(5) << e << endl;//2 cout << "int_distance3=" << setw(5) << setprecision(2) << e << endl; //2 /****************************************************************************************************************/ /****************************************************************************************************************/ float f = a / b; cout << "float" << endl; cout << "float_distance1=" << setw(5) << f << endl; //2.3 cout << "float_distance2=" << setw(5) << setprecision(0) << f << endl; //2.33333 cout << "float_distance3=" << setw(5) << setprecision(4) << f << endl; //2.333 cout << "float_distance4=" << setw(5) << setprecision(5) << f << endl; //2.3333 cout << "float_distance5=" << setw(5) << setprecision(2) << f << endl; //2.3 /****************************************************************************************************************/ /**************************************解释说明*****************************************************/ //单精度浮点数在机内占4个字节,用32位二进制描述。一个字节为八个位 //双精度浮点数在机内占8个字节,用64位二进制描述。 //浮点数在机内用指数型式表示,分解为:数符,尾数,指数符,指数四部分。 //数符占1位二进制,表示数的正负。 //指数符占1位二进制,表示指数的正负。 //尾数表示浮点数有效数字,0.xxxxxxx, 但不存开头的0和点 //指数存指数的有效数字。 //指数占多少位,尾数占多少位,由计算机系统决定。 //可能是数符加尾数占24位,指数符加指数占8位-- float. //数符加尾数占48位,指数符加指数占16位-- double. //知道了这四部分的占位,按二进制估计大小范围,再换算为十进制,就是你想知道的数值范围。 //对编程人员来说,double 和 float 的区别是double精度高,有效数字16位,float精度7位。 //但double消耗内存是float的两倍,double的运算速度比float慢得多,C语言中数学函数名称double 和 float不同, //不要写错,能用单精度时不要用双精度(以省内存,加快运算速度)。 }

【2】数字任意小数显示并保存

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include "pch.h" #include <iostream> #include <sstream> #include <iomanip> using namespace std; //精度转换函数 float round(float src, int bits){ stringstream ss; ss << fixed << setprecision(bits) << src; ss >> src; return src; } int main() { float src = 1.123456789; float f = round(src, 2); cout << f << endl; //1.12 cin.get(); }

【3】参考文献

setprecision、fixed、showpoint的用法总结(经典!!超经典!!)

C / C++ 保留两位小数(setprecision(n)的一些用法总结)

 


最新回复(0)