How to add Decimal to String then Parse it

I have a calculator I’m making, but I can’t figure out how to add a decimal to the number used for calculation. I tried adding a “.” then converting it to a string, but it comes back an error.
here’s my script,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class CalculatorMan : MonoBehaviour
{

    public TextMeshProUGUI calculator, input1,input2,input3, symbol;
    private int buttonClicks, inputNum;
    private float firstNum, secondNum, calculation;
    private string calcNum = "0";
    private string symboler;

    private bool decimalAdded;

    private void Start()
    {
        calculator.text = calcNum;
    }
    public void AddNumber(int num)
    {
        if (buttonClicks < 3)
        {
            if(buttonClicks == 0)
            {
                calcNum = "";
            }
            buttonClicks++;
            calcNum += num.ToString();
            calculator.text = calcNum;
        }
    }

    public void AddDecimal()
    {
        if (!decimalAdded)
        {
            calcNum += ".";
            calculator.text = calcNum;
            decimalAdded = true;
        }
    }
    public void DeleteNumber()
    {
        if (buttonClicks > 0)
        {
            buttonClicks--;
            if(calcNum.Substring(0,calcNum.Length ) == ".")
            {
                decimalAdded = false;
            }
            calcNum = calcNum.Substring(0, calcNum.Length - 1);
            if (buttonClicks == 0)
            {
                calcNum = "0";
            }
            calculator.text = calcNum;
           
        }
    }

    public void ClearString()
    {
        decimalAdded = false;
        calcNum = "0";
        buttonClicks = 0;
        calculator.text = calcNum;
    }

    public void Enter()
    {
        inputNum++;
        if(inputNum == 1)
        {
            input1.text = calcNum;
            firstNum = int.Parse(calcNum);
            ClearString();

        }
        if(inputNum == 2)
        {
            input2.text = calcNum;
            inputNum = 0;
            secondNum = int.Parse(calcNum);
            ClearString();
        }
    }

    public void Calculate()
    {
        Enter();
        switch (symboler)
        {
            case "+":
            calculation = firstNum + secondNum;
                break;
            case "-":
                calculation = firstNum - secondNum;
                break;
            case "X":
                calculation = firstNum * secondNum;
                break;
            case "÷":
                calculation = firstNum / secondNum;
                break;
        }

        calculation = calculation * 100;
        input3.text = (Mathf.Round(calculation) / 100).ToString();
    }

    public void ChangeSymbol(string symbolz)
    {
        symbol.text = symbolz;
        symboler = symbolz;
        Enter();
    }
}

Visually, it appears to add a decimal, but I think it reads it as a period or something? I don’t know, it says,“Input string was not in correct format”

Thanks in advance.

Integers cannot have decimal points, so this will never work if there are decimal points in the “calcNum” string:int.Parse(calcNum);
Integers are whole numbers. There can be no fractional or decimal portion. You have to use floating point types such as float, double, or Decimal if you want to represent any numbers that have fractional/decimal parts.

1 Like

Oh, I’m dumb, I totally forgot I was parsing it to int lol, I changed it to float.Parse. Thanks!

1 Like