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 / C++ / MFC
  4. C -- Copy float to string

C -- Copy float to string

Scheduled Pinned Locked Moved C / C++ / MFC
help
12 Posts 6 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.
  • T ToughGuy9

    Hi Friends, I would like to know is there any otherway to copy a double to string in C. I see that it can be done by using sprintf(.. , "%f", .. ) but I want to know anyother way to do this. My problem is: If i run the sprintf (.., "%f", .. ) on any machine in which has decimal point as , (comma) instead of a . (dot) (like French) it is converting . to , in the string even if the double value is having .(dot) . I don't want it to happen. Ur help is highly appreciated.. Thanks a lot.. Regards, Leela

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #2

    std::istringstream ? my mistake, this is C++. use ftoa() as an alternative, but never forget that the number is the number. your problem is a matter of representation (the decimal character - '.' or ',' - is not stored in the number itself)


    Don't know where to start ?
    Refer the Forums Guidelines and ask a friend

    [VisualCalc 3.0][Flags Beginner's Guide]

    C M D 3 Replies Last reply
    0
    • T ToughGuy9

      Hi Friends, I would like to know is there any otherway to copy a double to string in C. I see that it can be done by using sprintf(.. , "%f", .. ) but I want to know anyother way to do this. My problem is: If i run the sprintf (.., "%f", .. ) on any machine in which has decimal point as , (comma) instead of a . (dot) (like French) it is converting . to , in the string even if the double value is having .(dot) . I don't want it to happen. Ur help is highly appreciated.. Thanks a lot.. Regards, Leela

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #3

      You have to display the number, HOW the systems wants to show the number. If you have problems with it, can can also replace the "," with a string replace.:rolleyes:

      Greetings from Germany

      1 Reply Last reply
      0
      • T toxcct

        std::istringstream ? my mistake, this is C++. use ftoa() as an alternative, but never forget that the number is the number. your problem is a matter of representation (the decimal character - '.' or ',' - is not stored in the number itself)


        Don't know where to start ?
        Refer the Forums Guidelines and ask a friend

        [VisualCalc 3.0][Flags Beginner's Guide]

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #4

        this is C we are talking about, not C++.


        Maximilien Lincourt Your Head A Splode - Strong Bad

        1 Reply Last reply
        0
        • T toxcct

          std::istringstream ? my mistake, this is C++. use ftoa() as an alternative, but never forget that the number is the number. your problem is a matter of representation (the decimal character - '.' or ',' - is not stored in the number itself)


          Don't know where to start ?
          Refer the Forums Guidelines and ask a friend

          [VisualCalc 3.0][Flags Beginner's Guide]

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #5

          He asked to do that in C, not in C++ :rolleyes:


          Cédric Moonen Software developer
          Charting control [v1.1]

          1 Reply Last reply
          0
          • T ToughGuy9

            Hi Friends, I would like to know is there any otherway to copy a double to string in C. I see that it can be done by using sprintf(.. , "%f", .. ) but I want to know anyother way to do this. My problem is: If i run the sprintf (.., "%f", .. ) on any machine in which has decimal point as , (comma) instead of a . (dot) (like French) it is converting . to , in the string even if the double value is having .(dot) . I don't want it to happen. Ur help is highly appreciated.. Thanks a lot.. Regards, Leela

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #6

            This is controle by the LOCALE. I'm not certain that you can force sprintf to a certain representation ( . instead of , ) ?


            Maximilien Lincourt Your Head A Splode - Strong Bad

            D 1 Reply Last reply
            0
            • T toxcct

              std::istringstream ? my mistake, this is C++. use ftoa() as an alternative, but never forget that the number is the number. your problem is a matter of representation (the decimal character - '.' or ',' - is not stored in the number itself)


              Don't know where to start ?
              Refer the Forums Guidelines and ask a friend

              [VisualCalc 3.0][Flags Beginner's Guide]

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #7

              toxcct wrote:

              use ftoa() instead

              Such as this implementation?


              "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

              "Judge not by the eye but by the heart." - Native American Proverb

              1 Reply Last reply
              0
              • T ToughGuy9

                Hi Friends, I would like to know is there any otherway to copy a double to string in C. I see that it can be done by using sprintf(.. , "%f", .. ) but I want to know anyother way to do this. My problem is: If i run the sprintf (.., "%f", .. ) on any machine in which has decimal point as , (comma) instead of a . (dot) (like French) it is converting . to , in the string even if the double value is having .(dot) . I don't want it to happen. Ur help is highly appreciated.. Thanks a lot.. Regards, Leela

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #8

                ToughGuy9 wrote:

                If i run the sprintf (.., "%f", .. ) on any machine in which has decimal point as , (comma) instead of a . (dot) (like French) it is converting . to , in the string even if the double value is having .(dot) . I don't want it to happen.

                If I understand you correctly, you want numbers formatted like 123,456.78 regardless of the locale. Is that correct?


                "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                "Judge not by the eye but by the heart." - Native American Proverb

                T 1 Reply Last reply
                0
                • M Maximilien

                  This is controle by the LOCALE. I'm not certain that you can force sprintf to a certain representation ( . instead of , ) ?


                  Maximilien Lincourt Your Head A Splode - Strong Bad

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #9

                  Maximilien wrote:

                  I'm not certain that you can force sprintf to a certain representation ( . instead of , ) ?

                  Not until _sprintf_l() came along. That might not be an option for the OP, however.


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  1 Reply Last reply
                  0
                  • D David Crow

                    ToughGuy9 wrote:

                    If i run the sprintf (.., "%f", .. ) on any machine in which has decimal point as , (comma) instead of a . (dot) (like French) it is converting . to , in the string even if the double value is having .(dot) . I don't want it to happen.

                    If I understand you correctly, you want numbers formatted like 123,456.78 regardless of the locale. Is that correct?


                    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                    "Judge not by the eye but by the heart." - Native American Proverb

                    T Offline
                    T Offline
                    ToughGuy9
                    wrote on last edited by
                    #10

                    Hi David, I want the double to be stored into the integer string as with decimal point as dot (.) but NOT as a , comma. Regards, Leela

                    D 1 Reply Last reply
                    0
                    • T ToughGuy9

                      Hi Friends, I would like to know is there any otherway to copy a double to string in C. I see that it can be done by using sprintf(.. , "%f", .. ) but I want to know anyother way to do this. My problem is: If i run the sprintf (.., "%f", .. ) on any machine in which has decimal point as , (comma) instead of a . (dot) (like French) it is converting . to , in the string even if the double value is having .(dot) . I don't want it to happen. Ur help is highly appreciated.. Thanks a lot.. Regards, Leela

                      T Offline
                      T Offline
                      ToughGuy9
                      wrote on last edited by
                      #11

                      Hi, Thanks to One and all for trying to help me in this regard,, As of now after copying the double to the string i.e., after sprintf statement Iam checking if string has , using strchr().. If i find a ,(comma) iam replacing it with a . (dot). But if can find some other alternative in which i can copy the value in the double variable as it is to a String.. Btw the double variable has value with .(dot) - Leela

                      1 Reply Last reply
                      0
                      • T ToughGuy9

                        Hi David, I want the double to be stored into the integer string as with decimal point as dot (.) but NOT as a , comma. Regards, Leela

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #12

                        But why would you want to do this for countries that format numbers differently?


                        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                        "Judge not by the eye but by the heart." - Native American Proverb

                        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