How to convert format of number from English to Italian with VBScript?

Viewed 84

I need to convert the English numbering to Italian.

I tried using multiple replacements but I don't think it's the right method.

price = Replace(price, ".", ",")

The problem is that when I have too large numbers I get replaced several times and the wrong numbers come up.

For example:

English version: 3,450.108
After replacement: 3,450,108 (but it's wrong)
Correct format: 3.450,102

2 Answers

If you want to swap 2 delimiter characters you need to use a temp character for the second one, otherwise you won't be able to distinguish between the original second delimiter and the replaced first one.

price = Replace(price, ".", "_")
price = Replace(price, ",", ".")
price = Replace(price, "_", ",")

Would not recommend manually replacing values in an attempt to get the correct format when VBScript can already do it for you using the SetLocale() Function in conjunction with the FormatNumber() Function which will return the string representation of that number for the specific locale.

Note: Remember that the actual value and how a value is displayed are two separate things (see the example below).

Option Explicit
Const decimalplaces = 3
Dim price: price = 3450.108 'This is the raw value from your data source.

Call SetLocale("en-gb")
Call WScript.Echo("English (UK) price: " & FormatNumber(price, decimalplaces))
Call SetLocale("it-it")
Call WScript.Echo("Italian price: " & FormatNumber(price, decimalplaces))

Output:

English (UK) price: 3,450.108
Italian price: 3.450,108
Related