C++中小数点的位数限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/798046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Digit limitation from decimal point in C++
提问by yalcin
I'm new in C++.I have double variable double a=0.1239857
I want to limit variable a
from decimal point two digits.So a
will be 0.12
. I know C++ have functions that return largest or smallest integer that is greater or lower than a
like ceil or floor.Is there a function that implement digit limitation of floating-point variable? Or How can i change precision of a
variable?
我是 C++ 新手。我有双变量double a=0.1239857
我想限制a
小数点两位数的变量。所以a
将是0.12
. 我知道 C++ 有返回大于或小于a
ceil 或 floor 的最大或最小整数的函数。是否有实现浮点变量数字限制的函数?或者我怎样才能改变a
变量的精度?
Best regards...
此致...
回答by Alnitak
Are you actually trying to round the number, or just change its displayed precision?
您实际上是在尝试对数字进行四舍五入,还是只是更改其显示的精度?
For the former (truncating the extra digits):
对于前者(截断多余的数字):
double scale = 0.01; // i.e. round to nearest one-hundreth
value = (int)(value / scale) * scale;
or (rounding up/down as appropriate, per jheriko's answer)
或(根据 jheriko 的回答酌情向上/向下舍入)
double scale = 0.01; // i.e. round to nearest one-hundreth
value = floor(value / scale + 0.5) * scale;
For the latter:
对于后者:
cout << setprecision(2) << value;
where the parameter to setprecision()
is the maximum number of digits to show after the decimal point.
其中参数 tosetprecision()
是小数点后显示的最大位数。
回答by bradtgmurray
This will result in two digits after the decimal place.
这将导致小数点后两位数。
a = floor(a * 100.0) / 100.0;
回答by David Cournapeau
What do you mean by you want to limit the variable ? The value or its formatting. For the value, you can use floor + division. Something like:
你想限制变量是什么意思?值或其格式。对于该值,您可以使用楼层 + 除法。就像是:
double a = 0.12123
double b;
b = floor(a * 100) / 100
回答by schnaader
If you just want to output the value, you can do something like
如果你只想输出值,你可以做类似的事情
printf("%.3f", a); // Output value with 3 digits after comma
If you want to convert the value itself, you can do:
如果要转换值本身,可以执行以下操作:
a = (int)(a * 1000) / 1000.0f;
Note that both do no rounding, they just truncate the value.
请注意,两者都没有四舍五入,它们只是截断了值。
回答by dirkgently
Use a ios_base::precision
for formatting i/o.
使用 aios_base::precision
来格式化 i/o。
回答by Brian Neal
You can set the precision on a stream, e.g.
您可以在流上设置精度,例如
double d = 3.14579;
cout.precision(2);
cout << d << endl;
// Or use a manipulator
#include <iomanip>
cout << setprecision(2) << d << endl;
Note that when you send a double or float to a stream like this, it will automatically round for you (which can trip you up sometimes if you aren't aware of this).
请注意,当您向这样的流发送双精度或浮点数时,它会自动为您舍入(如果您不知道这一点,有时可能会绊倒您)。
回答by jheriko
An actual rounding solution would be x = floor(100*x + 0.5) / 100;
assuming the value to be rounded is in a variable "x".
实际的舍入解决方案是x = floor(100*x + 0.5) / 100;
假设要舍入的值在变量“x”中。
The x = floor(100*x) / 100;
recommended by others here will actually truncate the number to 2dp instead.
在x = floor(100*x) / 100;
别人推荐这里实际上将截断号而不是2DP。
回答by Joe Schmoe
you could also do something like this:
你也可以做这样的事情:
//This code will ask the user for an input, set the decimal precision to the hundredths place, and add 4.63 to the inputted variable
int banana;
cin >> banana;
cout << setprecision(2) << fixed << banana + 4.63;