How to set number format to show all decimal digits in C++?
115 观看
1回复
6 作者的声誉
I have a problem in c++, I have this code to do a division:
long a = 33732193487;
int b = 1000000;
double c = (double)a/(double)b;
Well, I would like to store the complete result in the variable c, I mean:
c = 33732.193487;
for later, for example, use this value to make a sum or substrac. But I only get to store the result with one decimal.
Is there any option to store the 6 decimals with c++?
Thank you in advance! Sorry if my question is too obvious.
作者: Carlos Pascual Sanchez 的来源 发布者: 2017 年 12 月 27 日回应 1
1像
62391 作者的声誉
Let me guess (a complete program would have been better): you're seeing six decimal places when you write the value to standard out:
std::cout << c << '\n';
Those six decimal places are the default for output; they have nothing to do with the precision of the value of c
, which is determined by the type of c
. For double
you typically have about fifteen digits. To see those digits in your output statement, use std::setprecision
:
std::cout << std::setprecision(15) << c << '\n';
But beware: you can use values greater than 15 in the call to setprecision
and the stream inserter will happily generate more digits, with nonsensical values.
来自类别的问题 :
- c++ 理解指针有什么障碍,克服它们可以做些什么?
- c++ 在C ++中解析INI文件的最简单方法是什么?
- c++ 什么时候应该在C ++中使用“朋友”?
- c++ 你如何清除stringstream变量?
- variables 从Python中的函数返回多个值的最佳方法是什么?
- variables 在.CSS文件中创建变量以在该.CSS文件中使用
- variables 是否可以在标准C ++中打印变量的类型?
- variables JavaScript闭包如何工作?
- numbers 在JavaScript中验证十进制数 - IsNumeric()
- numbers 有没有一种简单的方法可以将数字转换为PHP中的单词?
- numbers 2个数字之间的差异
- numbers 生成数字的分区
- decimal Convert Decimal to Double?
- decimal 如何在C#中转换十六进制和十进制之间的数字?
- decimal 如何在Java中将数字舍入到n个小数位
- decimal 如何将十进制值四舍五入到小数点后两位(用于在页面上输出)
- division 双精度值返回0
- division 我如何强制划分为浮点?分部继续向下舍入到0?
- division Python整数除法产生浮点数
- division 除法结果始终为零