Get Current Input Language NumberDecimalSeparator?

Hobbyist_programmer 621 Reputation points
2022-04-14T14:51:40.437+00:00

Hallo,

I am trying to get NumberDecimalSeparator for the current input language.

I have a system with English (OS language) and I have a german keyboard.

following code still gives ". " dot as a decimal separator but the right one is ",". How do i get the decimal separator from the keyboard?.

InputLanguage.CurrentInputLanguage.Culture.NumberFormat.NumberDecimalSeparator

I have textboxes in my application and i want to allow the numbers with only "," or "." depending on the keyboard or language settings. How do i do that?.

Thanks

Developer technologies VB
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2022-04-14T15:07:12.103+00:00

    The settings come from what the user's locale settings are, not any attached keyboard. In the user's local settings they specify what the decimal separator (and other values) are to use. Again, this is completely independent of any keyboard characters.

    To get the current UI settings (based upon the user's settings) then you use CultureInfo.CurrentUICulture. This is the system settings overridden with any user-customized settings. There is a related property CultureInfo.CurrentCulture which is the non-UI variant. Honestly I don't know why you'd ever need it.

    Once you have the CultureInfo for the UI then NumberFormat.CurrentDecimalSeparator is what you want.

       ' Get current user's decimal separator  
       Dim separator = System.Globalization.CultureInfo.CurrentUICulture.NumberFormat.CurrentDecimalSeparator  
    

    Note that most UIs automatically handle using the UI culture so you don't need to do anything special. You'd only need it if you wanted to do custom parsing but also note that the framework can handle this as well.

       Dim text = "1.234"  
       Dim result As Double  
         
       If (Double.TryParse(text, NumberStyles.Any., CultureInfo.CurrentUICulture, [ByRef] result))  
          # Valid double  
       End If  
    

    You can customize NumberStyles to limit the options.

    Alternatively consider using a MaskedTextBox if your UI framework supports it.

    1 person found this answer helpful.

  2. Castorix31 90,521 Reputation points
    2022-04-15T15:49:58.067+00:00

    Maybe you can test with NLS API
    Like :

                Dim sbDataUser As StringBuilder = New StringBuilder(128)
                If (GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SDECIMAL, sbDataUser, sbDataUser.Capacity) <> 0) Then
                    Dim sbDataSystem As StringBuilder = New StringBuilder(128)
                    If (GetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT, LOCALE_SDECIMAL, sbDataSystem, sbDataSystem.Capacity) <> 0) Then
                        MessageBox.Show(String.Format("Decimal Separator (User) : {0}{1}Decimal Separator (System) : {2}", sbDataUser.ToString(), vbCrLf, sbDataSystem.ToString()), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    End If
                End If
    

    with declarations :

    <DllImport("Kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>
    Public Shared Function GetLocaleInfoEx(lpLocaleName As String, LCType As UInteger, lpLCData As StringBuilder, cchData As Integer) As Integer
    End Function
    
    Public Const LOCALE_NAME_USER_DEFAULT = Nothing
    Public Const LOCALE_NAME_INVARIANT = ""
    Public Const LOCALE_NAME_SYSTEM_DEFAULT = "!x-sys-default-locale"
    
    Public Const LOCALE_SDECIMAL = &HE  ' Decimal separator, eg "." For 1,234.00
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.