I need to do some math work with numbers from a text file that are formatted based on culture, so I could get 1,234.56 or I could get 1.234,56 or even 1 234,56 I think. I had originally thought to get the current culture with
$culture = (Get-Culture).Name
Then convert all numbers to en-US format, do my math, and convert back to the original format. However, something as simple as this isn't working.
$useCulture = New-Object System.Globalization.CultureInfo("en-US")
$value1 = "1,234.56"
$value2 = "1234,56"
[double]$value1 = $value1.ToString("f", $useCulture)
[double]$value2 = $value2.ToString("f", $useCulture)
$value1
$value2
I would expect to get back 1234.56 for both values, but the second one is coming back as 123456, so obviously I am not understanding how ToString is working. My guess is that it assumes what it has is in en-US format, because that is what my machine is configured to, and it is then trying to convert to the same thing, and on $value2 it just assumes the thousands separator was in the wrong place? In any case, how would I go about converting from an arbitrary format to a specified format so I can be sure sums are correct, then convert back to the original arbitrary format? Or, am I totally missing something and there is a much better way to approach this? Also, if at all possible I would like to be able to do this using PS 2.0, since I can't depend on target machines being upgraded to newer versions.