Search code examples
.netvb.netlocalizationglobalizationaspose

Questions on localization/globalization of Ctype & .ToString


A few questions I need answered to help debug an issue I'm having with globalization/localization that's leaving me rather stumped.

1) How exactly does CType work if I convert a string into a double when it comes to globalization/localization?

From my research, and asking other developers in the office, I think CType should use whatever culture settings you have on your computer. So for example CType("1.23", Double) would work fine for a US culture computer. But it would mess up by converting it to 123 on say a Netherland's culture computer where , is recognized as the decimal point and . is recognized as the thousand separator. But if the string was 1,23, then it would work fine on the Netherlands computer and become 123 on the US computer.

2) How does .ToString work if I grab a number and say doubleNumber.ToString to convert it to a string?

From my experience here, I'm 99.9% sure that there should be no thousand separators included, so a number like 1,234.56 would be turned into 1234.56 on a US computer, correct? And on a Netherlands culture computer that number would become 1234,56 because , is the decimal place, right?

3) Is there a way on a Netherlands culture computer that CType(#, Double) or .ToString on a double could somehow mess up and start recognizing . as the decimal separator instead of a , like Netherland culture is supposed to recognize?

Bottom Line: Item #3 is where I'm having my problem. 99.9% of my users, US and foreign, can run my software fine without any localization issues. But a user in the Netherlands reported a bug where importing a number from a txt file somehow was converting 1,23 into 123. When one of our customer support people told them to change the decimal point to a . it worked fine. Short of that Netherlands user for some reason using a US culture computer (which they insist they were not) I can't figure out how this issue is occurring.


Solution

  • You are right in your assumptions. .NET will use the culture of the current computer.

    The way to apply a specific culture is:

    Dim str As String = "97.9"
    Dim num As Double = Double.Parse(str, Globalization.CultureInfo.InvariantCulture)
    

    And the same to convert a number to string:

    num = 97.9
    str = num.ToString(Globalization.CultureInfo.InvariantCulture)
    

    You can specify the needed culture, see this MSDN Documentation.