Search code examples
unity-game-engineinput-fieldunity-ui

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


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 !


Solution

  • 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;
            }