Results 1 to 15 of 15

Thread: [RESOLVED] Decimal separator

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Decimal separator

    What (API?) function can I use to retrieve the decimal separator character from a specific computer (which is defined according to regional settings)?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: Decimal separator

    Just read the registry value from HKEY_CURRENT_USER\Control Panel\International. The decimal separator is sDecimal.

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Decimal separator

    Quote Originally Posted by gigel_hifi View Post
    Just read the registry value from HKEY_CURRENT_USER\Control Panel\International. The decimal separator is sDecimal.
    Thanks!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: [RESOLVED] Decimal separator

    or ...

    MsgBox Mid$(Format$(0.1, "fixed"), 2, 1) & " is the decimal separator on this computer"

  5. #5
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: [RESOLVED] Decimal separator

    Hold on a sec, it should be possible to achieve what you want without knowing what the locale preferences are, you might be making work for yourself. VB has a set of functions that expect/create numerical strings using the locale preferences as well as functions that expect/create using the US default dot.
    W o t . S i g

  6. #6

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Decimal separator

    Quote Originally Posted by Magic Ink View Post
    or ...

    MsgBox Mid$(Format$(0.1, "fixed"), 2, 1) & " is the decimal separator on this computer"
    Wow, how didn't I think of that? Thanks!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  7. #7
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: [RESOLVED] Decimal separator

    Here's another function using pure vb code only:

    Code:
    Public Function DecimalSep() As String
       DecimalSep = Format$(0.1, ".")
    End Function

  8. #8

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Decimal separator

    Quote Originally Posted by Milk View Post
    Hold on a sec, it should be possible to achieve what you want without knowing what the locale preferences are, you might be making work for yourself. VB has a set of functions that expect/create numerical strings using the locale preferences as well as functions that expect/create using the US default dot.
    I'm trying to convert values stored as strings in a text file which represent floating point numbers, i.e. after reading the string I must use CSng(Str) or Val(Str) or CSng(Replace(Str),",",".") or... whatnot.
    Whenever I think I've taken care of all possible pitfalls there's always some situation for which my code does not make a correct conversion.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  9. #9

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Decimal separator

    Quote Originally Posted by DrUnicode View Post
    Here's another function using pure vb code only:

    Code:
    Public Function DecimalSep() As String
       DecimalSep = Format$(0.1, ".")
    End Function
    This is becoming ever more simple, thanks
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  10. #10
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: [RESOLVED] Decimal separator

    This may be of interest;

    Code:
    Function dpChar$()
            dpChar$ = Mid$(Format$(0.1, "fixed"), 2, 1)
        End Function
        Function Val#(ByVal txt$)
            'Wraps VBA.Val
            ' makes VAL able to handle decimal point chars other than "."
            ' VBA val only works on decimals when the decimal char is "."
            Static normal As Boolean, init As Boolean, dp$
            If Not init Then
                dp$ = dpChar()
                normal = (dp$ = ".")
                init = True
            End If
            If normal Then
                Val = VBA.Val(txt$)
            Else
                Val = VBA.Val(Replace(txt$, dp$, "."))
            End If
            'VB help recommends use of CDbl but..
            'the following does'nt work with strings that contain numbers and text
            ' like eg. "5.4 mp" because the error is tripped and 0 is returned
            'On Error Resume Next
            'Val = CDbl(txt$)
        End Function

  11. #11

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Decimal separator

    @Magic Ink:

    The code you've posted is more or less my intended approach.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  12. #12
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: [RESOLVED] Decimal separator

    Don't forget to remove the thousand separator (if any) before dealing with the decimal separator:

    Code:
    Option Explicit
    
    Public Function DecimalSep() As String
       DecimalSep = Format$(0, ".")
    End Function
    
    Public Function ThousandsSep() As String
       ThousandsSep = Mid$(Format$(1000, "#,#"), 2, 1)
    End Function
    
    Public Function Val(ByVal strText As String) As Double
       Val = VBA.Val(FixString(strText))
    End Function
    
    Private Function FixString(ByVal strText As String) As String
       FixString = Replace$(Replace$(strText, ThousandsSep, ""), DecimalSep, ".")
    End Function
    
    Private Sub Form_Load()
       Debug.Print DecimalSep, ThousandsSep, Val(1234.56)
    End Sub
    Last edited by DrUnicode; Feb 9th, 2011 at 09:46 AM. Reason: revised ThousandsSep

  13. #13

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Decimal separator

    Quote Originally Posted by DrUnicode View Post
    Don't forget to remove the thousand separator (if any) before dealing with the decimal separator:

    Code:
    Option Explicit
    
    Public Function DecimalSep() As String
       DecimalSep = Format$(0, ".")
    End Function
    
    Public Function ThousandsSep() As String
       ThousandsSep = Format$(0, ",")
    End Function
    
    Public Function Val(ByVal strText As String) As Double
       Val = VBA.Val(FixString(strText))
    End Function
    
    Private Function FixString(ByVal strText As String) As String
       FixString = Replace$(Replace$(strText, ThousandsSep, ""), DecimalSep, ".")
    End Function
    
    Private Sub Form_Load()
       Debug.Print DecimalSep, ThousandsSep, Val(1234.56)
    End Sub
    Both DecimalSep and ThousandsSep yield a
    ","
    Are the format statements correct?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  14. #14
    Fanatic Member DrUnicode's Avatar
    Join Date
    Mar 2008
    Location
    Natal, Brazil
    Posts
    631

    Re: [RESOLVED] Decimal separator

    Sorry. Try this:

    Code:
    Public Function ThousandsSep() As String
       ThousandsSep = Mid$(Format$(1000, "#,#"), 2, 1)
    End Function

  15. #15

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] Decimal separator

    Quote Originally Posted by DrUnicode View Post
    Sorry. Try this:

    Code:
    Public Function ThousandsSep() As String
       ThousandsSep = Mid$(Format$(1000, "#,#"), 2, 1)
    End Function
    All right now, thank you.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width