简体   繁体   English

减去C#中的百分比

[英]Subtract percentage in C#

I'm trying to subtract percentage in C# using: 我试图使用以下方法减去C#中的百分比:

n = n - (n * 0.25);

but I'm getting an error: 但我收到一个错误:

"Cannot implicitly 'double' to 'int'. An explicit conversions exists (are you missing a cast?)" “不能隐含'加倍'到'int'。存在显式转换(你是否错过了演员?)”

Your value n is an int . 你的值n是一个int

When you multiply by 0.25 ( which is a double ), the resulting value is a double that you try to assign to a int . 当您乘以0.25 (这是一个double )时,结果值是您尝试分配给intdouble

To solve it, you have to specify that you are aware that you will lose precision using "explicit conversion". 要解决它,您必须指定您意识到使用“显式转换”将失去精度。

n = n - (int)(n * 0.25);

Doing (Type)value is called "to cast value to Type ". 执行(Type)value称为“将valueType ”。 This is exactly what the error message suggest you to do. 这正是错误消息建议您执行的操作。

Or, if you don't want to keep the precision, declare n not as an int but as a double . 或者,如果您不想保持精度,请将n声明为int而不是double In this case, you will not have to cast n * 0.25 to int . 在这种情况下,您不必将n * 0.25int

If you don't want to switch back and forwards between int and double types you could just use: 如果您不想在int和double类型之间前后切换,可以使用:

n = (n * 75) / 100 n =(n * 75)/ 100

if your answer ever has decimals they'll be lost though 如果你的答案有小数,他们就会丢失

Your variable n must be an integer, but the result of your calculation is a double, since it involves multiplication by a double ( 0.25 ). 您的变量n必须是整数,但计算结果是双精度,因为它涉及乘以double( 0.25 )。

You can cast the result back to an int like this: 您可以将结果转换回int,如下所示:

n = (int)(n - (n * 0.25));

I'm assuming that n is an integer type then, say int , as you don't give a clue to that. 我假设n是一个整数类型,然后说int ,因为你没有给出一个线索。 In which case the easiest solution is to do: 在这种情况下,最简单的解决方案是:

n = Convert.ToInt32(n - (n * 0.25));

Or you can cast: 或者你可以施放:

n = (int)(n - (n * 0.25));

Check the type of variable 'n'. 检查变量'n'的类型。

Either 'n' should be of double type. 'n'应该是双重类型。

Or 要么

Use explicit cast to convert to int. 使用显式转换转换为int。

int n = (int)(n - (n * 0.25));

Your n variable is an int . 你的n变量是一个int When you try to multiple with 0.25 , 0.25 is double , so result will be double . 当您尝试使用0.25倍数时, 0.25double ,因此结果将double You should cast it manually because there is no Implicit Numeric Conversion for double to int . 您应该手动Implicit Numeric Conversion它,因为对于doubleint没有Implicit Numeric Conversion You have to use Explicit Numeric Conversion for them. 您必须为它们使用Explicit Numeric Conversion

From --> To

double --> sbyte , byte, short, ushort, int, uint, long, ulong, char, float, or decimal

You should convert your right expression to int . 您应该将右表达式转换为int

int n = 100;
n = (int) (n - (n * 0.25));
Console.WriteLine(n);

Here is a DEMO . 这是一个DEMO

And remember; 记住;

  • Explicit numeric conversion may cause loss of precision. 显式数字转换可能会导致精度损失。
  • When you convert from a double value to an integral type, the value is truncated. double值转换为整数类型时,该值将被截断。 If the resulting integral value is outside the range of the destination value, the result depends on the overflow checking context. 如果结果积分值超出目标值的范围,则结果取决于溢出检查上下文。

你必须将结果转换为int

n=(int)(n-(n*0.25));

try: 尝试:

n = n - (int)((double)n * 0.25);

note: by doing this you wont have numbers behind the point in the n result. 注意:通过这样做,你不会在n结果中的点后面有数字。

I guess this could be an issue with the type of n being int it least needs to be double 我想这可能是一个问题,n的类型是int它最不需要double

hence when you have n = n - (n * 0.25) the result is a double 因此当你有n = n - (n * 0.25) ,结果是双倍的

if you want to cast it as int then beware of rounding since it would not always be ending in .00 如果你想把它作为int转换,那么要注意舍入,因为它不会总是以.00结尾

Also i think this would be better n = n * 0.75 另外我认为这会更好n = n * 0.75

The best way would be to do 最好的方法是做

 n = n - n/4;

If you want a percentage to be a whole number between 0 and 100, otherwise you should declare n to be a double by replacing int n with double n . 如果你想有一个百分比为0和100之间的整数,否则你应该申报n是一个double替换int ndouble n No costly conversion will occur in the proposed assignment. 建议的任务不会发生代价高昂的转换。 Note that n/4 is an integer because both operands ( n and 4 ) are integers, causing no promotions, thus using integer division. 注意, n/4是一个整数,因为两个操作数( n4 )都是整数,不会导致促销,因此使用整数除法。

Explanation 说明

This is type promotion, n is multiplied by a double , which promotes n*0.25 automatically to a double . 这是类型提升, n乘以double ,它会自动将n*0.25提升为double A primitive can only be promoted into a higher rank, not demoted to a lower rank. 原语只能被提升到更高的等级,而不是降级到更低的等级。 A primitive x is of a higher rank then another primitive y if it can hold all values of y without causing loss of precision. 如果原始x可以保持y所有值而不会导致精度损失,则它具有更高的等级,然后是另一个原始y A double can hold all values of an integer, but an integer can, for example, not hold 0.1 . double可以保存整数的所有值,但是整数可以例如不保持0.1 So you are trying to promote and demote. 所以你正试图推广和降级。 See MSDN library for more information. 有关更多信息,请参阅MSDN库

Note: Casting from a double to an int causes the value to be truncated, that is all decimals after the 'dot' will be erased, so -2.5 becomes -2 and 1.5 becomes 1 . 注意:从铸造doubleint导致要被截断的值,即“点”毕竟小数将被擦除,所以-2.5变为-21.5变为1 Integer division, as used above also rounds to zero, making this assignment equal with your assignment. 如上所用,整数除法也会舍入为零,使得此赋值与您的赋值相等。 But avoiding any costly conversions. 但避免任何昂贵的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM