Canvas Input field with decimal comma instead of decimal point Input

HI,

I want to enter a decimal comma value in an input field instead of a decimal point value. I looked everywhere for a solution for C#, but found nothing. Who can give me a tip?

Input field accept any text. What stops you from just typing in somewhat like 10,01 ?

I’m not quite sure what you are looking for. If you need to convert a number to a string with a decimal comma, use .ToString(format), with Format being the correct culture as described by Microsoft.

If if you need to read a string that uses a deciaml comma and want to convert it to a number, use .Parse(Format) with Format beng the correct culture that uses a decimal comma, e.g.

CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");

will create a culture that can parse a decimal comma string, like so

string val = "1.234,56";
double number = Double.Parse(val, culture);
1 Like

guys, guys, I think what he meant was enter a decimal number with comma in an input field that has been set as Decimal Number in the inspector. I’m having the same issue. You can’t even TYPE the comma.

1 Like

Same problem with Unity 2020.1.8. Cannot enter a decimal with a comma although decimals are correctly shown with comma.

I´m not sure if this will work without doing/coding your own input field. The point and comma topic depends of the culture(info), because it´s different from country to country. I think the unity input fields only allow the american way with a point.

*This is just an assumption and not proved!

I have the same problem.

Did anyone find a solution for this? I have a released game with input fields set to Decimal Number and some players in Europe are telling me that when they put in “4.3”, after exiting the field it changes to “43”, but they’re not allowed to type a comma either so they just can’t put in decimals. It’s stupid.

3 years later, same issue. lol it used to work perfectly, with the phone X, changed the android device after 1 broke and now i cant even type it xD

I have solved this, at least on windows, in Europe. Not very thoroughly tested, but works with decimal inputfields for me. I had to get the keycode “266”, which is the comma key on the numpad and force the inputfield to accept that. (Note that this might be a different keycode for other devices, so just check that first if it does not work). then we do some conversion after. Just drop this on your TMP_inputfield.

using UnityEngine;
using TMPro;
using UnityEngine.EventSystems;
using System.Text.RegularExpressions;

public class InputFieldConvertCommaToPeriod : MonoBehaviour, IPointerClickHandler
{
    private TMP_InputField inputField;

    void Start()
    {
        inputField = GetComponent<TMP_InputField>();
        if (inputField.contentType == TMP_InputField.ContentType.DecimalNumber)
        {
            inputField.onValueChanged.AddListener(OnValueChanged);
            inputField.onEndEdit.AddListener(OnEndEdit);
        }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        // Activate this input field
        inputField.ActivateInputField();
    }

    void Update()
    {
        // Listen for specific keypress
        if (inputField.isFocused && Input.GetKeyDown((KeyCode)266)) // KeyCode for numpad comma
        {
            // Insert a period at the caret position
            int caretPos = inputField.caretPosition;
            string newText = inputField.text.Insert(caretPos, ".");
            inputField.text = newText;

            // Move the caret position forward
            inputField.caretPosition = caretPos + 1;
        }
    }

    void OnValueChanged(string value)
    {
        // Replace ',' with '.'
        if (value.Contains(","))
        {
            inputField.text = value.Replace(",", ".");
        }
    }

    void OnEndEdit(string value) //this is in case the user wrote something like "1.3.4" or "4.", which trust me, they will.
    {
        // Use regex to find valid decimal numbers, remove extra periods
        var regex = new Regex(@"\d+(\.\d{1,2})?");
        var match = regex.Match(value);
        if (match.Success)
        {
            inputField.text = match.Value;
        }
        else
        {
            // Set to some default value or clear the input
            inputField.text = "";
        }
    }
}

Anyone who finds this thread:
Using this code:

using UnityEngine;
using System.Globalization;
using System.Threading;

public class ForceEnglish : MonoBehaviour
{
    void Awake()
    {
        CultureInfo cultureInfo = new CultureInfo("en-US");
        Thread.CurrentThread.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentUICulture = cultureInfo;
    }
}

Will force your application to English, and will therefore accept dots as seperators, ALWAYS.
Hope this helps,
CleeDotExe

2 Likes

You literally saved me, thanks!

1 Like

The question was comma instead of punctuation why are the answers for punctuation?
If ContentType in Inputfield is set to decimal, we cannot enter any comma even if there’s pad shown in devices with commas

You probably can if you, using my steps, force the application language to a language that uses commas instead of points, unsure though.