0

e.g.

Converting between radians and degrees, 7 Pi radians = 1260 degrees

User input for radians.

Trying to convert a user input string "Pi" to Math.Pi, to calculate.

  • Can you post the relevant sample of code? If I understand correctly, you could set a constant to pi using `const decimal pi = 3.14159;` or `var pi = Math.Pi;` – Ash Jul 16 '16 at 05:14
  • So the user is actually inputting "0.5 Pi" if he wants 90°? – derpirscher Jul 16 '16 at 05:16
  • Sounds like you're after an [equation evaluator](http://stackoverflow.com/questions/333737/evaluating-string-342-yield-int-18). It's doable, but I'd recommend picking one of the libraries that do the heavy lifting for you. – Anon Coward Jul 16 '16 at 05:25

2 Answers2

0

EDIT:

Looks like you need to strip the string out and then use a formula.

Something like:

const string pi = "pi";
var input = "7pi";
var containsPi = (input.IndexOf(pi) > -1)
var inputDecimal = Convert.ToDecimal(input.Replace(input, pi, ""));
if (containsPi ) inputDecimal = inputDecimal * Math.Pi;
var resultInDegreees = inputDecimal * 180.0 / Math.Pi;
Ash
  • 5,786
  • 5
  • 22
  • 42
0

That depends on the input you allow the user.

If it's just something like 0.5 Pi or 3 Pi and you want the actual value for it, you can simply remove the Pi part from the input string, parse the remaining string into a number and multiply it with Math.PI

Regex pi = new Regex(@"\s*pi\s*", RegexOptions.IgnoreCase);
var v = pi.Replace(input, "");
double d = Convert.ToDouble(v);
double rad = d * Math.Pi;

If you have more complex inputs like 3/4 Pi or even more complex formulas, you'll need some sort of formula-interpreter. Many of them would be able to deal with constants like Pi.

derpirscher
  • 14,418
  • 3
  • 18
  • 35