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. CString::Format

CString::Format

Scheduled Pinned Locked Moved C / C++ / MFC
question
32 Posts 11 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 Bravoone_2006

    How can i make this : i have this code : csText.Format("%d", nSum); ok but my result is lets say 44,ok but if i have 22.3+22.3=44.6(nSum) this is not working : csText.Format("%d", nSum); How can i make this?(to have the result whit 44.6 lets say not 44)

    Bravoone

    C Offline
    C Offline
    CPallini
    wrote on last edited by
    #3

    %d is the type format specifier for signed decimal decimal, for floaing point numbers you can use, for instance, %g. see http://msdn2.microsoft.com/en-us/library/hf4y5e3w(VS.80).aspx[^] (remember that CString::Format behaves like sprintf). :)

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

    _ 1 Reply Last reply
    0
    • C CPallini

      %d is the type format specifier for signed decimal decimal, for floaing point numbers you can use, for instance, %g. see http://msdn2.microsoft.com/en-us/library/hf4y5e3w(VS.80).aspx[^] (remember that CString::Format behaves like sprintf). :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #4

      CPallini wrote:

      for floaing point numbers you can use, for instance, %g. see

      :doh:

      Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      C B 2 Replies Last reply
      0
      • _ _AnsHUMAN_

        CPallini wrote:

        for floaing point numbers you can use, for instance, %g. see

        :doh:

        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #5

        Don't you like %g? :-D

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        B 1 Reply Last reply
        0
        • _ _AnsHUMAN_

          CPallini wrote:

          for floaing point numbers you can use, for instance, %g. see

          :doh:

          Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

          B Offline
          B Offline
          Bravoone_2006
          wrote on last edited by
          #6

          ok,but i need 2 digits 44.66 lets say... then what ?

          Bravoone

          C C X 3 Replies Last reply
          0
          • C CPallini

            Don't you like %g? :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            B Offline
            B Offline
            Bravoone_2006
            wrote on last edited by
            #7

            "%g" is not working !How can i do that ?

            Bravoone

            C T 2 Replies Last reply
            0
            • B Bravoone_2006

              ok,but i need 2 digits 44.66 lets say... then what ?

              Bravoone

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

              Use this format tag: %.2f instead.


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

              1 Reply Last reply
              0
              • B Bravoone_2006

                ok,but i need 2 digits 44.66 lets say... then what ?

                Bravoone

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #9

                Don't you read documentation, do you? :mad: try: csText.Format("%.2f", nSum); BTW nSum is a misnaming for a float if you are using Hungarian Notation. :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                B 1 Reply Last reply
                0
                • B Bravoone_2006

                  "%g" is not working !How can i do that ?

                  Bravoone

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #10

                  On my system it works pretty well, anyway, see [^] or [^]. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                  1 Reply Last reply
                  0
                  • C CPallini

                    Don't you read documentation, do you? :mad: try: csText.Format("%.2f", nSum); BTW nSum is a misnaming for a float if you are using Hungarian Notation. :-D

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                    B Offline
                    B Offline
                    Bravoone_2006
                    wrote on last edited by
                    #11

                    csText.Format("%.2f", nSum); is not working why? i have read documentation but is not working !any why ?

                    Bravoone

                    C 1 Reply Last reply
                    0
                    • B Bravoone_2006

                      ok,but i need 2 digits 44.66 lets say... then what ?

                      Bravoone

                      X Offline
                      X Offline
                      Xing Chen
                      wrote on last edited by
                      #12

                      try like this: float uSum = 12.457 + 345; CString strFomat; strFomat.Format(TEXT("%.2f"),uSum);

                      B 1 Reply Last reply
                      0
                      • B Bravoone_2006

                        csText.Format("%.2f", nSum); is not working why? i have read documentation but is not working !any why ?

                        Bravoone

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #13

                        Bravoone_2006 wrote:

                        csText.Format("%.2f", nSum); is not working why?

                        What do you mean with the generic is not working? :confused: Please detail the misbehaviour (i.e. obtained result vs expected result). BTW Have you correctly declared nSum as floating point number? :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

                        H 1 Reply Last reply
                        0
                        • X Xing Chen

                          try like this: float uSum = 12.457 + 345; CString strFomat; strFomat.Format(TEXT("%.2f"),uSum);

                          B Offline
                          B Offline
                          Bravoone_2006
                          wrote on last edited by
                          #14

                          How can i do that ? lets say in list i have 44.22+44.22 the result must be in csText 88.44 HOW can i do that? int nSum = 0; int uSum = 0; for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++) { if(m_list1.GetCheck(/*index of item*/nRow)) { csText = m_list1.GetItemText( nRow, 2 ); uSum += atoi( csText); csText.Format("%.2f",uSum); m_sum.SetWindowText(csText); } } this code give the result : lets say i have in list 44.22+44.22 the result in csText is 88.00 but i need 88.44 HOW ?

                          Bravoone

                          _ X 2 Replies Last reply
                          0
                          • B Bravoone_2006

                            How can i do that ? lets say in list i have 44.22+44.22 the result must be in csText 88.44 HOW can i do that? int nSum = 0; int uSum = 0; for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++) { if(m_list1.GetCheck(/*index of item*/nRow)) { csText = m_list1.GetItemText( nRow, 2 ); uSum += atoi( csText); csText.Format("%.2f",uSum); m_sum.SetWindowText(csText); } } this code give the result : lets say i have in list 44.22+44.22 the result in csText is 88.00 but i need 88.44 HOW ?

                            Bravoone

                            _ Offline
                            _ Offline
                            _AnsHUMAN_
                            wrote on last edited by
                            #15

                            don't use atoi(). instead use atof() I think you must read the documentation rather than starting to build on the project. atoi converts a string to int and not to a float and then when you add them it returns an int in uSum which is just printed as a float value.

                            Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                            B T 2 Replies Last reply
                            0
                            • B Bravoone_2006

                              How can i do that ? lets say in list i have 44.22+44.22 the result must be in csText 88.44 HOW can i do that? int nSum = 0; int uSum = 0; for( int nRow = 0; nRow < m_list1.GetItemCount();nRow++) { if(m_list1.GetCheck(/*index of item*/nRow)) { csText = m_list1.GetItemText( nRow, 2 ); uSum += atoi( csText); csText.Format("%.2f",uSum); m_sum.SetWindowText(csText); } } this code give the result : lets say i have in list 44.22+44.22 the result in csText is 88.00 but i need 88.44 HOW ?

                              Bravoone

                              X Offline
                              X Offline
                              Xing Chen
                              wrote on last edited by
                              #16

                              you must declare uSum as float. and atof() instead of atoi(). float nSum = 0; float uSum = 0;

                              1 Reply Last reply
                              0
                              • _ _AnsHUMAN_

                                don't use atoi(). instead use atof() I think you must read the documentation rather than starting to build on the project. atoi converts a string to int and not to a float and then when you add them it returns an int in uSum which is just printed as a float value.

                                Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                                B Offline
                                B Offline
                                Bravoone_2006
                                wrote on last edited by
                                #17

                                ok but... : uSum += atof( csText); warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data Why?

                                Bravoone

                                O _ C T C 5 Replies Last reply
                                0
                                • B Bravoone_2006

                                  ok but... : uSum += atof( csText); warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data Why?

                                  Bravoone

                                  O Offline
                                  O Offline
                                  Optimus Chaos
                                  wrote on last edited by
                                  #18

                                  because nSum has data type int. :)

                                  1 Reply Last reply
                                  0
                                  • B Bravoone_2006

                                    ok but... : uSum += atof( csText); warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data Why?

                                    Bravoone

                                    _ Offline
                                    _ Offline
                                    _AnsHUMAN_
                                    wrote on last edited by
                                    #19

                                    uSum is an int. so you would need to use typecasting or declare uSum as a float. I think you are missing on the basics. :|

                                    Bravoone_2006 wrote:

                                    ok but... : uSum += atof( csText); warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data

                                    It would be better if you read some tutorials on typecasting, using variables and reading some more basics and some more............................and a few more....................:rolleyes:

                                    Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                                    1 Reply Last reply
                                    0
                                    • B Bravoone_2006

                                      ok but... : uSum += atof( csText); warning C4244: '+=' : conversion from 'double' to 'int', possible loss of data Why?

                                      Bravoone

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

                                      Bravoone_2006 wrote:

                                      Why?

                                      You declare an integer, how would you expect to store a floating point value into it and expect that it still holds a floating point value ? I think you REALLY need to find some good book about the basics of C++. I mean, knowing what an integer and a double are, is at least the bare minimum. It's the thing you learn at very very begining.


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

                                      1 Reply Last reply
                                      0
                                      • B Bravoone_2006

                                        "%g" is not working !How can i do that ?

                                        Bravoone

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

                                        Bravoone_2006 wrote:

                                        How can i do that ?

                                        by reading the CString::Format() documentation ! :zzz:


                                        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                        R 1 Reply Last reply
                                        0
                                        • _ _AnsHUMAN_

                                          don't use atoi(). instead use atof() I think you must read the documentation rather than starting to build on the project. atoi converts a string to int and not to a float and then when you add them it returns an int in uSum which is just printed as a float value.

                                          Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

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

                                          _AnShUmAn_ wrote:

                                          I think you must read the documentation rather than starting to build on the project

                                          no, you forget the prerequisite step : learn C++ :doh: this guy is f*cking too lazy and keeps asking since months now dumb questions


                                          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                          _ 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