Home > other >  Unity - TMP Input Field : How to limit number of digits after decimal point in Decimal Type
Unity - TMP Input Field : How to limit number of digits after decimal point in Decimal Type

Time:08-02

Is there any easy way to limit the number of digits after the decimal point with an TMP_InputField set at decimal type ?

For exemple, if the user type 12.347, I want the text to stop at 12.34

Thank you !

CodePudding user response:

You can register to an event onValidateInput, parse the string to a float, and reformat it with $"{value:0.##}.

CodePudding user response:

EDIT: If anyone else is looking for the exact same thing as me, I succeeded by doing this little trick :

        if (_hasComa)
        {
            string[] charAfterComa = _inputField.text.Split(",");
            string strAfterComa = charAfterComa[1];

            for (int i = 0; i < strAfterComa.Length; i  )
            {
                if (i >= 2)
                {
                    int index = strAfterComa.LastIndexOf(strAfterComa[i]);
                    if (index >= 0)
                        strAfterComa = strAfterComa.Substring(0, index);
                }
            }

            _inputField.text = charAfterComa[0]   ','   strAfterComa;
        }
  • Related