SizeF and InvariantCulture
-
What results do you think following code produces:
SizeF size = new SizeF(100.12f, 50.34f);
string result1 = Convert.ToString(size, CultureInfo.InvariantCulture);
string result2 = Convert.ToString(size, CultureInfo.GetCultureInfo("en-US"));
string result3 = size.ToString();
string result4 = FormattableString.Invariant($"{size}");{Width=100.12, Height=50.34}
, of course!? But, alas, the result is always:{Width=100,12, Height=50,34}
with a decimal comma instead of a decimal point. As a side note:"{" + string.Format(CultureInfo.InvariantCulture, "Width={0},Height={1}", size.Width, size.Height) + "}"
returns the expected result. That happens on my Windows 7 machine with a German OS. Now I am a little confused... I know that there's aSizeFConverter
, but that would make things too complicated. I have a function which receives anobject
, converts that to astring
and transfers thestring
to a different machine where the value should be parsed to aSizeF
again. (This post could also fit into the "Weird and Wonderful" category...Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
-
What results do you think following code produces:
SizeF size = new SizeF(100.12f, 50.34f);
string result1 = Convert.ToString(size, CultureInfo.InvariantCulture);
string result2 = Convert.ToString(size, CultureInfo.GetCultureInfo("en-US"));
string result3 = size.ToString();
string result4 = FormattableString.Invariant($"{size}");{Width=100.12, Height=50.34}
, of course!? But, alas, the result is always:{Width=100,12, Height=50,34}
with a decimal comma instead of a decimal point. As a side note:"{" + string.Format(CultureInfo.InvariantCulture, "Width={0},Height={1}", size.Width, size.Height) + "}"
returns the expected result. That happens on my Windows 7 machine with a German OS. Now I am a little confused... I know that there's aSizeFConverter
, but that would make things too complicated. I have a function which receives anobject
, converts that to astring
and transfers thestring
to a different machine where the value should be parsed to aSizeF
again. (This post could also fit into the "Weird and Wonderful" category...Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
Convert.ToString doesn't have a specific overload that takes a SizeF, so it goes to the default Object version. SizeF does not support IConvertable or IFormattable, so Convert.ToString doesn't really know what to do with it! So it ends up just calling value.ToString which uses the default formatting for the machine and ignores the format you provided. See the reference source and you'll see what I mean: Reference Source[^]
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
Convert.ToString doesn't have a specific overload that takes a SizeF, so it goes to the default Object version. SizeF does not support IConvertable or IFormattable, so Convert.ToString doesn't really know what to do with it! So it ends up just calling value.ToString which uses the default formatting for the machine and ignores the format you provided. See the reference source and you'll see what I mean: Reference Source[^]
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
Thanks - so this is a good fit for the Weird and Wonderful!
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
-
Thanks - so this is a good fit for the Weird and Wonderful!
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
You're welcome!
Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
What results do you think following code produces:
SizeF size = new SizeF(100.12f, 50.34f);
string result1 = Convert.ToString(size, CultureInfo.InvariantCulture);
string result2 = Convert.ToString(size, CultureInfo.GetCultureInfo("en-US"));
string result3 = size.ToString();
string result4 = FormattableString.Invariant($"{size}");{Width=100.12, Height=50.34}
, of course!? But, alas, the result is always:{Width=100,12, Height=50,34}
with a decimal comma instead of a decimal point. As a side note:"{" + string.Format(CultureInfo.InvariantCulture, "Width={0},Height={1}", size.Width, size.Height) + "}"
returns the expected result. That happens on my Windows 7 machine with a German OS. Now I am a little confused... I know that there's aSizeFConverter
, but that would make things too complicated. I have a function which receives anobject
, converts that to astring
and transfers thestring
to a different machine where the value should be parsed to aSizeF
again. (This post could also fit into the "Weird and Wonderful" category...Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
Useful ? ^] if the problem is transmitting the float value independently of local culture settings, serialization to binary, xml,json >? cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot