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. globalization / localization commas and periods

globalization / localization commas and periods

Scheduled Pinned Locked Moved C#
csharphelpquestion
9 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.
  • G Offline
    G Offline
    Glen Harvy 0
    wrote on last edited by
    #1

    Hi, Can someone help me with this scenario please. I need to hard code the comparison of a double with the double value returned from a datatable. The user is using a language that has the comma as a decimal separator. double dbValue = if(dbValue < 2.1) then blah blah blah How do I hard code the "2.1" value or is there some way to globally have .net do this. Thanks,

    G 1 Reply Last reply
    0
    • G Glen Harvy 0

      Hi, Can someone help me with this scenario please. I need to hard code the comparison of a double with the double value returned from a datatable. The user is using a language that has the comma as a decimal separator. double dbValue = if(dbValue < 2.1) then blah blah blah How do I hard code the "2.1" value or is there some way to globally have .net do this. Thanks,

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Glen Harvy wrote:

      The user is using a language that has the comma as a decimal separator.

      C# still uses period as decimal separator. A literal double value is always written as 2.1 in the code, regardless of any cultural settings.

      Despite everything, the person most likely to be fooling you next is yourself.

      G 1 Reply Last reply
      0
      • G Guffa

        Glen Harvy wrote:

        The user is using a language that has the comma as a decimal separator.

        C# still uses period as decimal separator. A literal double value is always written as 2.1 in the code, regardless of any cultural settings.

        Despite everything, the person most likely to be fooling you next is yourself.

        G Offline
        G Offline
        Glen Harvy 0
        wrote on last edited by
        #3

        Thanks but it seems that my question may have been misleading. If the current culture is one that uses comma's as decimal separators, why does the following throw an error.. double x = ; // <-- no worries - returns 2,0 if(x > 2.1) <--- throws an error. double y = Double.Parse("2.1", System.Globalization.CultureInfo.InvariantCulture.NumberFormat); if(x > y) <---- will this fix above error????? Thanks,

        Glen Harvy

        G 1 Reply Last reply
        0
        • G Glen Harvy 0

          Thanks but it seems that my question may have been misleading. If the current culture is one that uses comma's as decimal separators, why does the following throw an error.. double x = ; // <-- no worries - returns 2,0 if(x > 2.1) <--- throws an error. double y = Double.Parse("2.1", System.Globalization.CultureInfo.InvariantCulture.NumberFormat); if(x > y) <---- will this fix above error????? Thanks,

          Glen Harvy

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Glen Harvy wrote:

          double x = ; // <-- no worries - returns 2,0

          Where do you get the value from?

          Glen Harvy wrote:

          if(x > 2.1) <--- throws an error.

          Why do you think that? There is nothing wrong with that line.

          Despite everything, the person most likely to be fooling you next is yourself.

          G 1 Reply Last reply
          0
          • G Guffa

            Glen Harvy wrote:

            double x = ; // <-- no worries - returns 2,0

            Where do you get the value from?

            Glen Harvy wrote:

            if(x > 2.1) <--- throws an error.

            Why do you think that? There is nothing wrong with that line.

            Despite everything, the person most likely to be fooling you next is yourself.

            G Offline
            G Offline
            Glen Harvy 0
            wrote on last edited by
            #5

            Sorry I put the source of x in brackets. x is obtained from a datatable. The value of which is a double and as the culture is SK, I assume it's returned as 2,0 .

            Glen Harvy

            V G 2 Replies Last reply
            0
            • G Glen Harvy 0

              Sorry I put the source of x in brackets. x is obtained from a datatable. The value of which is a double and as the culture is SK, I assume it's returned as 2,0 .

              Glen Harvy

              V Offline
              V Offline
              Vikram A Punathambekar
              wrote on last edited by
              #6

              Irrespective of the culture settings, if you have a double, comparing it like if(d > 2.1) will work as expected*. Can you show some more code? * Not exactly, but for a different reason. You should instead use if((d - 2.1) < Double.Epsilon)

              Cheers, Vikram.


              The hands that help are holier than the lips that pray.

              1 Reply Last reply
              0
              • G Glen Harvy 0

                Sorry I put the source of x in brackets. x is obtained from a datatable. The value of which is a double and as the culture is SK, I assume it's returned as 2,0 .

                Glen Harvy

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                Glen Harvy wrote:

                x is obtained from a datatable. The value of which is a double and as the culture is SK, I assume it's returned as 2,0 .

                No, as it's a double it's not formatted into something like "2,0". It's just a numierical value, and there isn't a decimal separator at all. It's first when you format a number into a string that a decimal separator character is used.

                Despite everything, the person most likely to be fooling you next is yourself.

                G 1 Reply Last reply
                0
                • G Guffa

                  Glen Harvy wrote:

                  x is obtained from a datatable. The value of which is a double and as the culture is SK, I assume it's returned as 2,0 .

                  No, as it's a double it's not formatted into something like "2,0". It's just a numierical value, and there isn't a decimal separator at all. It's first when you format a number into a string that a decimal separator character is used.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  G Offline
                  G Offline
                  Glen Harvy 0
                  wrote on last edited by
                  #8

                  OK... How is the "2.1" being converted to a numeric then? In other words, the hard coded part of the equation? I don't see how .Net can recognise it as a double because it hasn't got a "," in it. What's good for the goose must be good for the gander :-)

                  Glen Harvy

                  G 1 Reply Last reply
                  0
                  • G Glen Harvy 0

                    OK... How is the "2.1" being converted to a numeric then? In other words, the hard coded part of the equation? I don't see how .Net can recognise it as a double because it hasn't got a "," in it. What's good for the goose must be good for the gander :-)

                    Glen Harvy

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    Glen Harvy wrote:

                    I don't see how .Net can recognise it as a double because it hasn't got a "," in it.

                    The C# language always uses period for decimal separator, regardless of any culture settings. When you write a literal value in the code, it's always written the same.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    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