将美国数字格式转换为欧盟

时间:2018-02-15 09:06:34

标签: excel number-formatting

是否可以将美国数字格式(逗号为千位分隔符)转换为EU(点数为千分隔符),其中包含公式或函数,如EU对美国的NUMBERVALUE(A2,“,”,“。”)?

(例如5,000.15至5.000,15)

我知道有一个选项可以使用或不使用系统分隔符(高级选项),但在这种情况下,公式中的“,”在“;”中更改作为参数分隔符,我想避免这种情况。

我在考虑将数值转换为字符串,然后使用右和中间函数来添加分隔符,但我无法成功地执行此操作。

感谢。

修改 所以这就是我的想法。这不是最佳解决方案'在将其再次转换为数值之前,原因值不能用于进一步的计算(字符串)。

首先,使用此函数将数值转换为str(函数写得很差,我知道)所以它至少看起来像欧盟数字格式。

Function ConvertToEU(s As String, decpl As Integer)


'Checks if integer
Dim pos As Integer
pos = InStr(s, ".")

Dim decpart As String: decpart = ""
Dim LArray() As String
Dim temp1

If pos <> 0 Then
LArray = Split(s, ".")
temp1 = LArray(0)
decpart = LArray(1)
Else
temp1 = s
End If

Dim dectemp As Integer: dectemp = decpl - Len(decpart)


If dectemp > 0 Then
For i = 1 To dectemp
decpart = decpart & 0
Next

ElseIf dectemp < 0 Then

decpart = Left(decpart, decpl)

Else

End If


Dim LenSTR As Double: LenSTR = Len(temp1) / 3

Dim cond As Boolean

If CStr(CLng(LenSTR)) = LenSTR Then
cond = True
Else: cond = False
End If

Dim start As String: start = ""
Dim rest As String
If cond = False Then
start = Left(temp1, Len(temp1) - Int(LenSTR) * 3) & "."
rest = WorksheetFunction.Substitute(temp1, Left(temp1, Len(temp1) - Int(LenSTR) * 3), "")
Else
rest = temp1
End If

Dim x As Integer: x = 1
Dim temp As String
Dim conv As String
For i = 1 To Int(LenSTR) - 1
If cond = False Then
Debug.Print "X"
conv = conv & Mid(rest, x, 3) & "."
temp = temp & Mid(rest, x, 3)
x = x + 3
Else
conv = conv & Mid(rest, x, 3) & "."
temp = temp & Mid(rest, x, 3)
x = x + 3
End If

Next


If Len(decpart) > 0 Then
ConvertToEU = start & conv & Right(rest, 3) & "," & decpart
Else
ConvertToEU = start & conv & Right(rest, 3)
End If


End Function

编辑2: 已修正小数位。

再次将其转换为数值

Function ConvertToVal(s As String)

ConvertToVal = CDbl(WorksheetFunction.Substitute(WorksheetFunction.Substitute(s, ".", ""), ",", "."))

    End Function

是否可以正确地执行此操作,将值保持为数字?

0 个答案:

没有答案