Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SizeF and InvariantCulture

SizeF and InvariantCulture

Scheduled Pinned Locked Moved C#
questionlearning
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #1

    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 a SizeFConverter, but that would make things too complicated. I have a function which receives an object, converts that to a string and transfers the string to a different machine where the value should be parsed to a SizeF again. (This post could also fit into the "Weird and Wonderful" category...

    Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

    OriginalGriffO B 2 Replies Last reply
    0
    • B Bernhard Hiller

      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 a SizeFConverter, but that would make things too complicated. I have a function which receives an object, converts that to a string and transfers the string to a different machine where the value should be parsed to a SizeF again. (This post could also fit into the "Weird and Wonderful" category...

      Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      B 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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!

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        Thanks - so this is a good fit for the Weird and Wonderful!

        Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

        OriginalGriffO 1 Reply Last reply
        0
        • B Bernhard Hiller

          Thanks - so this is a good fit for the Weird and Wonderful!

          Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • B Bernhard Hiller

            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 a SizeFConverter, but that would make things too complicated. I have a function which receives an object, converts that to a string and transfers the string to a different machine where the value should be parsed to a SizeF again. (This post could also fit into the "Weird and Wonderful" category...

            Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups