1

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 !

Deltaaaa
  • 11
  • 3

3 Answers3

0

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

Iggy
  • 4,767
  • 5
  • 23
  • 34
  • Thanks !! I will try this when I am at home. But it will change only when I will enter or leave the field, so I guess it’s impossible to limit it “in live” while the user is typing ? – Deltaaaa Aug 01 '22 at 18:12
0

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;
        }
Deltaaaa
  • 11
  • 3