|
-
Feb 9th, 2011, 03:18 AM
#1
[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)
-
Feb 9th, 2011, 03:54 AM
#2
Member
Re: Decimal separator
Just read the registry value from HKEY_CURRENT_USER\Control Panel\International. The decimal separator is sDecimal.
-
Feb 9th, 2011, 04:53 AM
#3
Re: Decimal separator
 Originally Posted by gigel_hifi
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)
-
Feb 9th, 2011, 05:20 AM
#4
Re: [RESOLVED] Decimal separator
or ...
MsgBox Mid$(Format$(0.1, "fixed"), 2, 1) & " is the decimal separator on this computer"
-
Feb 9th, 2011, 06:01 AM
#5
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.
-
Feb 9th, 2011, 06:08 AM
#6
Re: [RESOLVED] Decimal separator
 Originally Posted by Magic Ink
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)
-
Feb 9th, 2011, 06:10 AM
#7
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
-
Feb 9th, 2011, 06:13 AM
#8
Re: [RESOLVED] Decimal separator
 Originally Posted by Milk
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)
-
Feb 9th, 2011, 06:16 AM
#9
Re: [RESOLVED] Decimal separator
 Originally Posted by DrUnicode
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)
-
Feb 9th, 2011, 06:21 AM
#10
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
-
Feb 9th, 2011, 06:50 AM
#11
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)
-
Feb 9th, 2011, 07:34 AM
#12
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
-
Feb 9th, 2011, 08:29 AM
#13
Re: [RESOLVED] Decimal separator
 Originally Posted by DrUnicode
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)
-
Feb 9th, 2011, 09:45 AM
#14
Re: [RESOLVED] Decimal separator
Sorry. Try this:
Code:
Public Function ThousandsSep() As String
ThousandsSep = Mid$(Format$(1000, "#,#"), 2, 1)
End Function
-
Feb 10th, 2011, 03:24 AM
#15
Re: [RESOLVED] Decimal separator
 Originally Posted by DrUnicode
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|