Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Calculate multiple numbers from one Text Input?

Discussion in 'Scripting' started by Exarpo, Aug 24, 2020.

  1. Exarpo

    Exarpo

    Joined:
    Nov 25, 2019
    Posts:
    51
    So let's say I have a text input where I need to input a number, such as 6, and another input where i can add another number such as 8 - then i want to multiply them, that's is not a problem at all as they are in two different text inputs and can be treated as two variables. But what would I do if I had just one text input where the user has to input "6x8" instead of hacing multiple ones, how would I then calculate it?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You will have to go through the input string character by character and parse out the numbers and the mathematical operation being used, then perform the calculation.

    It kinda depends on how complicated you want to allow the expressions to be. If it's only ever two numbers, and it's only ever one arithmetic operation, it's pretty simple: you just read digits until you get to an operator, figure out what kind of operator it is, then keep reading digits till the end to get the second number, then perform the operation.

    If you want to allow arbitrary calculations, like
    8 + (65 * 3 - 12)
    , then you'll need to tokenize the input and create an expression tree, which you can then evaluate with some predefined order of operations. It's nontrivial.
     
    Brathnann likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    If you just need to support that specific format of input for specifically multiplying two numbers, you can use string.Split to divide the input string, and then parse result[0] and result[1] and multiply them, something like:
    Code (csharp):
    1. string input = "6x8";
    2. string[] components = input.Split("x"[0]);
    3. if (components.Length == 1) {
    4. return float.Parse(components[0]);
    5. }
    6. return float.Parse(components[0]) * float.Parse(components[1]);
    But I don't think that will really be enough, and odds are your users will be expecting other math expressions to work as well. To support that, you will need to use a math expression parser. If you google "C# math expression parser" there are huge piles of search results, some of which are guides on how to write one, some are ready-to-go packages. I recommend starting there.
     
    Brathnann likes this.
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    You need to pull the numbers out as well as the equation symbol.

    One possible option is to simply split on the operator (+, -, x, *, /). Then you'll need to loop through the string array you get and see if you have valid numbers. Assuming you have those numbers, you just have to figure out what operator they have put in and then do the calculations. But it also depends on how complex the operation can get to how much you'll need to do.
     
  5. Exarpo

    Exarpo

    Joined:
    Nov 25, 2019
    Posts:
    51
    I do want to allow more complicated calculations and if I get you right, you mean I should have some predefined orders that the operations can come in, I want i´the user to be able to input almost anything so I don't think that's a good idea. Tho I may have missunderstood you as the "tokenize the input and create an expression tree" part is a bit beyond my level xd.
     
  6. Exarpo

    Exarpo

    Joined:
    Nov 25, 2019
    Posts:
    51
    I will try googling that thanks :).
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Your best option is probably to follow what @StarManta said and integrate an existing math expression parser library into your project. Here's one: https://github.com/codingseb/ExpressionEvaluator
     
  8. Exarpo

    Exarpo

    Joined:
    Nov 25, 2019
    Posts:
    51
    Woosh didn't expect this to be 4200 lines of code complicated :/.
     
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    They wrote 4200 lines of code so you don't have to. Once you import the package, you only need 2 lines to use it.

    (You don't want to know how many lines of code Unity has....)
     
    PraetorBlue likes this.
  10. Exarpo

    Exarpo

    Joined:
    Nov 25, 2019
    Posts:
    51
    Yea yea, i know its not hard to implement. Just surprised me that itself was this complicated.
     
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Keep in mind that library supports a bunch of extra things that you probably don't need for your simple use case, such as bitwise operators and mappings to System.Math functions.
     
  12. Exarpo

    Exarpo

    Joined:
    Nov 25, 2019
    Posts:
    51
    Yeah... btw does it support exponentation as well? Just wanna make sure