Eri*_*ndi 3 vb6 localization type-conversion
我是一个很长时间的C#开发人员,最近不得不查看一些VB6代码.经过一番挖掘,我们发现,在某个地方,某种程度上,我们将数字存储为字符串.现在我们正在阅读它,我们的代码并没有很好地处理文化差异.这是一个例子:
Dim text as String
Dim myVal as Double
Set text = "1,123456"
'This sets the value of myVal to 1123456 on our system - incorrect
myVal = text
Set text = "1.123456"
'This sets the value of myVal to 1.123456 on our system - correct
myVal = text
Run Code Online (Sandbox Code Playgroud)
请记住,这是VB6而不是VB.NET,是否有任何内置的方法,函数,无论什么,可以使用特定的文化将字符串转换为double?或者至少,暗示我们可能正在处理不同格式的转换?
我们仍在挖掘如何编写价值,看看我们是否可以对流程进行逆向工程,以便为我们提供一致的结果.但是,我们的客户已经拥有(拥有)一种格式或另一种格式(或两者,我们正在检查......)的数据,因此此时正确的转换算法或实现将是更好的答案.
我过去曾经使用过这个快速而又脏的函数来从文本中获取双重函数.在阿根廷,人们有时使用点,有时使用逗号来分隔十进制值,因此该函数已准备好接受这两种格式.如果只有一个标点符号,则假定它是小数点分隔符.
function ConvertDBL( texto )
dim retval, SepDecimal, SepMiles
If texto = "" then texto = "0"
retval = replace(trim(texto), " ", "")
If cdbl("3,24") = 324 then
SepDecimal = "."
SepMiles = ","
else
SepDecimal = ","
SepMiles = "."
end if
If InStr( Retval, SepDecimal ) > 0 then
If InStr( Retval, SepMiles ) > 0 then
If InStr( Retval, SepDecimal ) > InStr( Retval, SepMiles ) then
Retval = replace( Retval, SepMiles, "" )
else
Retval = replace( Retval, SepDecimal, "" )
Retval = replace( Retval, SepMiles, SepDecimal )
end if
end if
else
Retval = replace( Retval, SepMiles, SepDecimal )
end if
ConvertDbl = cdbl( retval )
end function
Run Code Online (Sandbox Code Playgroud)