-4

I have a string

string  w =  y + "/" + fromRow[col2.ColumnName];

and w returns value like 0/5

"0/5" and i have to convert it on int32 . i tried

int.parse, but its showing error input string are not in correct format.

string w="0/5"
int m=int32.parse(W)

I have also tried convert.toint32 but it also showing error.

    string w="0/5"
    int m=Convert.ToInt32(W)
Mansfield
  • 14,445
  • 18
  • 76
  • 112
user2516261
  • 73
  • 1
  • 8
  • 3
    Would you like it converted to 05 (octal 5), 0, 5, what? – It'sNotALie. Aug 07 '13 at 12:37
  • 2
    What do you want it to return? – Kevin DiTraglia Aug 07 '13 at 12:38
  • 2
    What value should the `int` have, if you try to parse `0/5`? – MBender Aug 07 '13 at 12:38
  • 3
    Could everyone please stop down-voting and close-voting this. The question demonstrates a specific problem, and shows attempts for how the OP has tried to solve it. It may be lacking a little "clarification of requirements" but that can be solved with some simple comment probing – musefan Aug 07 '13 at 12:40
  • I have a string like 0/5 and i have to add this value in datatable coloumn but that column accepting int value so i want to convert this value in int to add as adatatable coloumn value – user2516261 Aug 07 '13 at 12:43
  • 1
    @user2516261: What "int" value are you expected to store in the database? `0/5` is *not* an integer value – musefan Aug 07 '13 at 12:44
  • I'm assuming 0/5 is supposed to represent a fraction, right? What happens if you have 1/5? Would you want to put 0, 0.2, or 1 in your database? Also, are you limited to working with this string or is there a way you can work with the two original values it was built from? – Michelle Aug 07 '13 at 12:46
  • i use this valu to show only in grid not in database if the value is 1 the it shoul show like 1/5 i dont want to divide this . i have to only show this on 0/5 fomate – user2516261 Aug 07 '13 at 12:49
  • @user2516261: You should just store the first number if the database (i.e. `y`), then you just format it each time you need to display it. You clearly have the second value stored for later use anyway – musefan Aug 07 '13 at 12:51
  • 1
    In that case, you need to change the column type in your database to string or store the two values in separate columns. You can't store a string or two int values in one int column. – Michelle Aug 07 '13 at 12:54
  • Possible duplicate (answer anyway) [here](http://stackoverflow.com/questions/8761531/string-fraction-to-double) – Sayse Aug 07 '13 at 12:57

4 Answers4

1

Parse them out separately, THEN divide.

int x = Int32.Parse(y);
int y = Int32.Parse(fromRow[col2.ColumnName]);
int m = x/y;

Though do you mean to use doubles? It doesn't make a lot of sense to use int here since you're dividing.

EDIT: Based off comments, you want to literally store 0/5 in the database. An integer doesn't hold a value like that.

I think you need to change your database to store a string instead which would allow you to store 0/5.

If you want to parse 0/5 from your database back out to 2 separate integers, use String.Split on the string, passing it /

tnw
  • 13,521
  • 15
  • 70
  • 111
  • 0/5 happens to be an int, but obviously the input could easily include other cases like 1/5. OP might want doubles or might want to floor/round/ceil, but would have to clarify. – Michelle Aug 07 '13 at 12:45
0

If the string "0/5" is only used for display purposes then you only need to store the two integer components that make up the display value. The you you use those two values to create the "display string" as required.

For example, if this was a user score for a quiz, then your two values are "user score" and "maximum score". Each of which would be stored as an int in two separate database fields.

Then anytime you need to display it you just recreate the string, for example:

string display = string.Format("{0}/{1}", userScore, maximumScore);
//where totalScore and maximumScore have been pulled from your DB

Of course, in the example of a quiz, you would likely be able to recalculate the "maximum score" from other data anyway (e.g. Number of Questions) so there would only be a need to store the first value, userScore.

In your example case, just store the value of y in the database.

musefan
  • 47,875
  • 21
  • 135
  • 185
0

An integer is a single number with no decimal part, e.g., 3, 28, -76, 154, etc. You have a string, "0/5". "0/5" doesn't represent a number (especially as you've said it's not a fraction), so it can't be converted to an integer because it's not the same kind of data. You'll need to change the data type of the column you want to store it in, or store your data in a different way (e.g. store the two integers in separate columns).

Michelle
  • 2,830
  • 26
  • 33
-2
   Try this: 
  string line = "1/5";
  int idx = line.IndexOf('/');
  Console.WriteLine(idx);
  int a = Int32.Parse(line.Substring(0, idx));
  Console.WriteLine(a);

  int b = Int32.Parse(line.Substring(idx+1, line.Length - (idx+1)));
  Console.WriteLine(b);
  decimal result = a/b;

            Console.WriteLine(result);