C-Code for replacing ',' in place of '.' (i.e. for ex 39.6 to 39,6)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sivadhanekula
    New Member
    • Nov 2008
    • 58

    #1

    C-Code for replacing ',' in place of '.' (i.e. for ex 39.6 to 39,6)

    Hi everyone

    I have a database of 24 tables all with 'dot separated numbers like 39.6' and I am extracted the required data in to one table which is also a dot separated data. But I need that data in comma separated value"i.e. in place of 39.6 i need 39,6"..Since I am in Germany here comma's and dot's are replaced. So can you give me a code so that if there are any dots in my input it should be converted to comma and should give it in my out put.

    Thank you
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    You can use sscanf() and sprintf(), something like this:
    [code=c]
    char num[] = "39.6";
    int p,d;
    sscanf(num,"%i. %i",p,d); // p=39 and d=6
    sprintf(num,"%i ,%i",p,d); // now num = "36,6"
    [/code]

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Another option is to investigate how locales work. I've never used this feature myself, but my understanding is that if you set the right locale then the numbers will naturally print out with commas instead of periods.

      Comment

      • sivadhanekula
        New Member
        • Nov 2008
        • 58

        #4
        can you tell me what a locale is...I have never heard of it and can you tell me how can I check that for solving this problem..
        Thank you

        Comment

        • sivadhanekula
          New Member
          • Nov 2008
          • 58

          #5
          Originally posted by arnaudk
          You can use sscanf() and sprintf(), something like this:
          [code=c]
          char num[] = "39.6";
          int p,d;
          sscanf(num,"%i. %i",p,d); // p=39 and d=6
          sprintf(num,"%i ,%i",p,d); // now num = "36,6"
          [/code]
          Thank you.....I have got the output with the above code...but with some changes....

          Comment

          • mac11
            Contributor
            • Apr 2007
            • 256

            #6
            Originally posted by sivadhanekula
            can you tell me what a locale is...I have never heard of it and can you tell me how can I check that for solving this problem..
            Thank you
            donbock is right - locales are the best way to solve your problem. I've used it before in a project that had to support multiple languages and numerical formats (like what you want to do with , and . )

            Have a look at these links - I just googled them up but they should give you an idea.

            http://www.w3.org/International/questions/qa-i18n

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by donbock
              Another option is to investigate how locales work. I've never used this feature myself, but my understanding is that if you set the right locale then the numbers will naturally print out with commas instead of periods.
              In an ideal world that would work; in a real world most of the time just the "C"
              locale is supported which prints the decimal point as a dot. The solution given
              above will work or otherwise the following little hack will do:

              Code:
              char* replaceXbyY(char* p, char x, char y) {
                 char* s;
                 for (s= p; s && *s; s++)
                    if (*s == x) *s= y;
                 return p;
              }
              kind regards,

              Jos

              Comment

              • sivadhanekula
                New Member
                • Nov 2008
                • 58

                #8
                Originally posted by JosAH
                In an ideal world that would work; in a real world most of the time just the "C"
                locale is supported which prints the decimal point as a dot. The solution given
                above will work or otherwise the following little hack will do:

                Code:
                char* replaceXbyY(char* p, char x, char y) {
                   char* s;
                   for (s= p; s && *s; s++)
                      if (*s == x) *s= y;
                   return p;
                }
                kind regards,

                Jos
                Hi...

                Can you give me a code for this in for loop 'like' in the for loop it should check all my data and if it is a point that point should be replaced by a comma

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by sivadhanekula
                  Hi...

                  Can you give me a code for this in for loop 'like' in the for loop it should check all my data and if it is a point that point should be replaced by a comma
                  No; the little 'replaceXbyY' function replaces all 'x's by 'y's in a string; I leave it
                  to your imagination how to use it for your output.

                  kind regards,

                  Jos

                  Comment

                  • sivadhanekula
                    New Member
                    • Nov 2008
                    • 58

                    #10
                    So, In place of X and Y can we keep '.' and ','. By initializing dot to X and Comma to Y.

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by sivadhanekula
                      So, In place of X and Y can we keep '.' and ','. By initializing dot to X and Comma to Y.
                      Sure; have a look at the parameter list: x and y are parameters and the function
                      replaces the value of every x by the value of y. Feel free to change that little
                      function.

                      kind regards,

                      Jos

                      Comment

                      • sivadhanekula
                        New Member
                        • Nov 2008
                        • 58

                        #12
                        I didn't understand the for loop in the given program....Can you explain it for me....

                        Regards
                        Siva

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by sivadhanekula
                          I didn't understand the for loop in the given program....Can you explain it for me....
                          Either parameter p is NULL or it isn't. If it is the condition 's && *s' fails immediately
                          and no loop body is executed. Otherwise pointer s traverses over all characters
                          pointed to by p and s until it reaches the '\0' character in which case the for loop
                          condition 's && *s' fails again.

                          kind regards,

                          Jos

                          Comment

                          Working...