-8

I know the % is a symbol that can't be used by itself but I'm trying to make a number with a percent.

For example:

 DeliveryGoal = 85%;

But Visual Studio tells me that this is an "invalid expression term". Is it a string or a type? Is there a conversion that I have to do? Also what declaration would percent symbols fall under? I'm using c#.

I'm not sure if I'm being specific but I'm not trying to get the remainder but rather the symbol itself to display next to that 85.

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
Crusader88
  • 19
  • 1
  • 5
  • 4
    Have you Googled anything? – dfundako Jan 03 '17 at 21:10
  • Yes, I phrased the question "declaring % symbol in c#". I also reworded and click on links but they don't point me to something directly. – Crusader88 Jan 03 '17 at 21:13
  • 1
    "I know the % is a symbol that can be used by itself" - nope, it's only the remainder operator... – Jon Skeet Jan 03 '17 at 21:14
  • @Crusader88 OK, then start reading basic c# docs... – L.B Jan 03 '17 at 21:14
  • If you looked up [what the percentage symbol does in C#](https://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx), you'll see that it's only a modulus operator. To decalre a percentage, like 85%, you would enter that in like `0.85`, or `85.0 / 100.0`. – Random Davis Jan 03 '17 at 21:15
  • That is an invalid expression because `%` or *modulo* is just "return the remainder of the left side divided by the right side." It needs both, if you want a percent, well, .85 just happens to be a percentage representing what you want. – silentsod Jan 03 '17 at 21:16
  • % is a Multiplicative operator, you cannot re-declare it. Try to add it as a symbol to string: $"{value} %" – Markiian Benovskyi Jan 03 '17 at 21:17
  • @MarkBenovsky what? *% is a Multiplicative operator,* ? – Thomas Ayoub Jan 03 '17 at 21:21
  • What is the data type of DeliveryGoal and what do you want to do with it? I would think assigning the "%" would be irrelevant if you are using that like an integer, you only need to show the "% symbol" when you display it ... in that case what you are trying to do is unnecessary in the assignment. – Theo Jan 03 '17 at 21:22
  • @ThomasAyoub https://msdn.microsoft.com/en-gb/library/6a71f45d.aspx#Anchor_2 – Markiian Benovskyi Jan 03 '17 at 21:25
  • @MarkBenovsky you can *overload* it, but it is a binary operator. – Cee McSharpface Jan 03 '17 at 21:28
  • I'm Sorry Guys, I'm not sure if I'm not being specific but I'm not trying to get the remainder but rather the symbol itself to display next to that 85. – Crusader88 Jan 03 '17 at 21:28
  • @MarkBenovsky It may be a multiplicative operator (as in it relates somehow to multiplication) but calling it such is incredibly confusing as it sounds like you are calling it the multiplication operator. – Abion47 Jan 03 '17 at 21:28
  • oops. so `DeliveryGoal` is a string meant for output/display? edit that into the question. – Cee McSharpface Jan 03 '17 at 21:28
  • @dlatikay Yes that is correct. – Crusader88 Jan 03 '17 at 21:29
  • @Crusader88: Don't worry about the downvote brigade, it is harsher than it needs to be. – Guvante Jan 03 '17 at 21:32
  • @MarkBenovsky thanks a lot for enlarging my vocabulary! – Thomas Ayoub Jan 03 '17 at 21:39

5 Answers5

1

Put your value in quotes "85%" to get C# to allow it. You should always do that for non-numbers meant for display.

For those trying to get 85% the number:

C# defines % as a binary operator similar to * that performs the remainder which is the leftover in division (negative numbers are weird though so watch out for them).

There is no concept of a number 85% since that is syntactically 85 % ___ where the underscore is an unspecified value.

If you want to numerically represent 85% you should instead write 0.85 or 85.0 / 100 if you want the percentage. (Note that you cannot do 85 / 100 since that is 0 due to integer division).

Guvante
  • 18,775
  • 1
  • 33
  • 64
  • Remainder and modulus aren't the same operation. They're similar, and related, but not the same. % is the remainder operator, not the modulus operator. – Servy Jan 03 '17 at 21:18
  • @Servy false. `%` *is* the modulus operator. – Thomas Ayoub Jan 03 '17 at 21:20
  • @ThomasAyoub Actually, he may be right. Formally, MSDN refers to `%` as the modulus operator, but according to [this topic](http://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder), the operator actually behaves as a remainder operation, not a modulus operation. (`int i = -21 % 4;` will result in `-1`.) – Abion47 Jan 03 '17 at 21:26
  • it may be [called modulus operator](https://msdn.microsoft.com/en-us/library/6a71f45d.aspx), but what it does is to [compute the remainder](https://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx) – Cee McSharpface Jan 03 '17 at 21:26
  • Just going to replace with remainder since the nuance is pointless in this case and that is the more commonly understood phrasing. Quick searching shows there isn't a consensus as to which is which honestly. – Guvante Jan 03 '17 at 21:28
0

% is used for modulus expression. It:

Divides the value of one expression by the value of another, and returns the remainder.

If you want to apply a percentage of a number, use a multiplication.

float DeliveryGoal = 0.85;
float totalCommands = 100; // Or whatever value
float totalDelivery = 80; // Or whatever value
if(totalDelivery /  totalCommands > DeliveryGoal)
    // Objective is reached
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
0

If DeliveryGoal is a string, then just declare it as a string literal: "88%"

If it is a double, then parse the double and add the symbol:

string percent = (val * 100).ToString() + "%";

Alternatively, you can have the ToString method format the double for you:

string percent = val.ToString("P");

See the MSDN page on double.ToString for more formatting options.

Abion47
  • 22,211
  • 4
  • 65
  • 88
  • I think what you posted is what I'm looking for, but I have to mess around with it a little to get what I need for the program I'm working on. – Crusader88 Jan 03 '17 at 21:46
  • @Crusader88 If you post some more details on how your program uses `DeliveryGoal`, we can write more specific answers to your question. – Abion47 Jan 03 '17 at 21:48
  • I'm very new to stack. I will do my best. I'm at work and they block a lot of sites. I cant copy and paste code into here, and a website like pastebin is blocked... – Crusader88 Jan 03 '17 at 21:53
  • Is there a way I can send you some code? Posing my question with it being visual would be best I think to help answer my question. – Crusader88 Jan 03 '17 at 22:06
0

To represent a percentage, you would of course divide by 100 when setting the value.

float myPercentage = 0.85;

Note that numeric variables in and of themselves do not contain any formatting information. A float is just a binary representation of the pure number, and as you (should) know 85% and 0.85 are equal and equivalent.

To display it with a % sign, you would use ToString() and the "p" format code.

Console.WriteLine(myPercentage.ToString("p"));
John Wu
  • 50,556
  • 8
  • 44
  • 80
0

Using % outside of a string is treated as an operator.

To get the percentage to display exactly like 85%, you want to use a string to include %.

DeliveryGoal = "85%";

Don Cheadle
  • 5,224
  • 5
  • 39
  • 54