3

If I had a string "5/2", how could I use float.Parse to get 2.5? When I do it inside Unity3D I get an Invalid Format error. It works for whole numbers, like "5" would get 5, but I'm making a graphing calculator and a lot of times the slope of line is a fraction.

Tre B
  • 81
  • 2
  • 8
  • 1
    Your trouble is this: `5/2` is not a valid number, but an expression you must evaluate. Coding the parser left as an exercise for the reader. – Deduplicator Apr 04 '14 at 19:41
  • You can check answers for this question: [c# evaluating string “3*(4+2)” yield int 18](http://stackoverflow.com/questions/333737/c-sharp-evaluating-string-342-yield-int-18) – Ulugbek Umirov Apr 04 '14 at 19:47
  • Another option is to implement the [Shunting-yard algorithm](http://en.wikipedia.org/wiki/Shunting-yard_algorithm) or to google for an existing solution. – Olivier Jacot-Descombes Apr 04 '14 at 19:51

2 Answers2

6

It is not a valid number, it is an expression which needs to be evaluated. you can do that using DataTable.Compute. You can evaluate more complex expressions too using this technique.

var result = new DataTable().Compute("5/2",null);

Note: Datatable is expensive, so you can create a instance or static member which holds the reference of DataTable for you.

Read more about compute in MSDN.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Did not know this one. Good to know ! – aloisdg Apr 04 '14 at 19:45
  • This one does not work in Unity because Unity doesn't use .NET 4.5. I'm accepting the other one for that reason, but thank you for the answer. I may use this in the future for other projects. – Tre B Apr 05 '14 at 09:22
  • @TreB Why do you need .Net4.5? This method exist in [.net 1.1 itself](http://msdn.microsoft.com/en-us/library/system.data.datatable.compute%28v=vs.71%29.aspx) – Sriram Sakthivel Apr 05 '14 at 09:39
  • Oh, my bad. It said .NET 4.5 on the Microsoft reference site. Also in Unity, unless I'm doing it wrong, it says I can't use the System.Data namespace because Data could not be found in System. I just assumed it was because of that. Sorry if I'm wrong. – Tre B Apr 05 '14 at 09:45
  • 1
    Nevermind, I hope this will help future readers :) Btw you need to add a reference to `System.Data.dll` and also a using reference to `System.Data` namespace to make it work. – Sriram Sakthivel Apr 05 '14 at 09:50
5

You'll need to split the string, parse the values individually and then do the division. So :

string[] tokens = input.Split('/');
float result = float.Parse(tokens[0])/float.Parse(tokens[1]);

Of course, you should add error handling to this, but that is "Proof of Concept" quality code.

aloisdg
  • 22,270
  • 6
  • 85
  • 105
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • OP asks for `Float.Parse()` solution, but you should consider to add a `Float.TryParse()` for everbody. No ? – aloisdg Apr 04 '14 at 19:43
  • I get the error `The best overloaded method match for 'string.Split(params char[])' has some invalid arguments` I just have what you have but put my string in there. – Tre B Apr 04 '14 at 19:46
  • @TreB I put an escape without thinking but that is only needed for backslashes, should be fixed using jones' edit. – evanmcdonnal Apr 04 '14 at 19:48
  • @aloisdg yes, my final sentence says this code is "Proof of Concept". The necessary error handling is out of scope for this question and I leave that to the OP. I'm not writing code for him, I'm explaining how to write the code. – evanmcdonnal Apr 04 '14 at 19:49
  • The only way it works for me is if I use ' instead of " in Split('/') – Tre B Apr 04 '14 at 19:58
  • 1
    @TreB Maybe a typo error. if you want to split with a string, use `input.Split(new string[] { "foo" }, StringSplitOptions.None)`. Check the [doc](http://msdn.microsoft.com/en-us/library/system.string.split.aspx). – aloisdg Apr 04 '14 at 20:18
  • 1
    @TreB yeah sorry, I did not run the code. There are several overloads of the `Split` method in the `String` class, the one you mention is probably the best and I've updated the answer to reflect that. – evanmcdonnal Apr 04 '14 at 20:23