Search code examples
c#doubledecimaldecimal-point

How to convert 2 numbers to 2 parts of a decimal number [C#]


Im trying to convert 2 user inputed numbers (for example):

double number = 5;
double number2 = 23;

into this:

decimalnumber = 5.23;

Solution

  • As to your actual problem about physical calculator - do not store it as numeric at all! Just use it like this:

    var userInput = new StringBuilder();
    ....
    userInput.Append(getUserNextChar());
    ....
    

    Then just parse this input when it is actually needed as numeric:

    decimal numeric;
    if(!decimal.TryParse(userInput.ToString(), out numeric))
    {
        throw new Exception("Invalid user input!");
    }
    //use your 'numeric' here