在 C++ 中获取数字的百分比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17405859/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:10:25  来源:igfitidea点击:

Get percent of number in c++

c++integer-division

提问by user1544067

How can I calculate several percent of int?
for example I want to get 30% from number, if I will use this example of code, I will get wrong answer:

如何计算 int 的几个百分比?
例如我想从数字中得到 30%,如果我使用这个代码示例,我会得到错误的答案:

int number = 250;
int result = (number / 100) * 30;  

result will be 60, and the real answer is 75, is there any way to calculate it?

结果会是60,而真正的答案是75,有没有办法计算它?

回答by Joni

Multiply before dividing:

除法前乘法:

int result = number * 30 / 100;

The reason you get the result you get is that division with integer types produces an integer result: 250 / 100 is 2. If you multiply before dividing you still get an integer result, but at least you haven't lost data in intermediate steps. If you have to deal with really huge numbers there is a danger of overflowing the range permitted by intthough.

得到结果的原因是整数类型的除法产生整数结果:250 / 100 是 2。如果在除法之前乘以仍然得到整数结果,但至少在中间步骤中没有丢失数据。如果您必须处理非常大的数字,则可能会超出允许的范围int

Alternatively you can use floating point arithmetic, where division can produce fractions of integers:

或者,您可以使用浮点运算,其中除法可以产生整数的分数:

int result = number * 0.30;

This may produce unexpected resultsthough, so you're better off using integers only as above. Or write 3.0/10instead of 0.30.

不过,这可能会产生意想不到的结果,因此最好仅使用上述整数。或者写3.0/10而不是0.30.

回答by Mats Petersson

Assuming the numbers are small(ish), you can just turn it around:

假设数字很小(ish),你可以把它转过来:

 int result = (number * 30) / 100;

(Parenthesis not required, but helps clarify).

(括号不是必需的,但有助于澄清)。

This won't work if either of the numbers are several million, but should be fine for numbers smaller than that.

如果其中一个数字是几百万,这将不起作用,但对于小于该数字的数字应该没问题。

回答by Sceptical Jule

Switching the operands (as others suggested) would work too, but just in case you do not want to, there's another solution:

切换操作数(如其他人建议的那样)也可以,但以防万一,还有另一种解决方案:

int number = 250;
int result = static_cast<double>(number) / 100 * 30; 

回答by CodeCrusader

Try this,

尝试这个,

int number = 250;
float result = number  * (float)(30/100.0);

回答by Ms. Nobody

Use float:

使用浮动:

float number = 250;
float result = (number / 100.0) * 30

Also just putting there the 100.0 with deciaml point might be enough.

也只需将 100.0 与 deciaml 点放在那里就足够了。

Because if you do it your way 250 / 100 in integer equals 20( you can put 100 in 200 only twice and integer doesn't care about the rest 50) and times 30 = 60.

因为如果你这样做,整数中的 250 / 100 等于 20(你只能将 100 放入 200 两次,整数不关心其余的 50),乘以 30 = 60。

回答by Emil Sit

You need to perform floating point math, otherwise the compiler does all the arithmetic here as integers.

您需要执行浮点数学运算,否则编译器会将此处的所有算术运算作为整数进行。

You might try

你可以试试

int result = (number / 100.0) * 30;

回答by user1544067

Now I think about some solution:

现在我想了一些解决方案:

int number = 250;
int result = number * 100;
result = (result / 100) * 30;
result /= 100;

回答by user3506720

The whole program is like:

整个程序是这样的:

double number , result;

cout << "Enter the number:";
cin >> number;

result = number * 30.0 / 100;
cout << "Result = " << result << endl;

and you will get your answer....

你会得到你的答案......

回答by marvin orsua

score/items*30 ex;

分数/项目*30 ex;

15%

15%

int score, items;
cout<<"input your score"<< score<<endl;
cin>>score;
cout<<"input number of items"<<items<<endl;
cin>>items;
float total=score/items*15
cout<<"you got "<<total<<""<<endl;

回答by Tim

Another possibility, good in Windows when working with 32 bit numbers.

另一种可能性,在 Windows 中处理 32 位数字时很好。

int num = 750;

整数编号 = 750;

int result;

整数结果;

result = MulDiv( num, 30, 100 );

结果 = MulDiv( num, 30, 100 );

https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-muldiv

https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-muldiv