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. Format retuening a negative value

Format retuening a negative value

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 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.
  • V Offline
    V Offline
    VCProgrammer
    wrote on last edited by
    #1

    Hi all, I am converting a double value to CString using format but it is giving negative value,

    double val3 = val2/(1024*1024);
    CString test_size;
    test_size.Format(_T("%d"),val3);
    AfxMessageBox(test_size);

    Can anybody please tell me what is the error. i am coding in vc2008

    R Y M C 4 Replies Last reply
    0
    • V VCProgrammer

      Hi all, I am converting a double value to CString using format but it is giving negative value,

      double val3 = val2/(1024*1024);
      CString test_size;
      test_size.Format(_T("%d"),val3);
      AfxMessageBox(test_size);

      Can anybody please tell me what is the error. i am coding in vc2008

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      What is val2?

      It is a crappy thing, but it's life -^ Carlo Pallini

      V S 2 Replies Last reply
      0
      • R Rajesh R Subramanian

        What is val2?

        It is a crappy thing, but it's life -^ Carlo Pallini

        V Offline
        V Offline
        VCProgrammer
        wrote on last edited by
        #3

        double

        double val0 = c*bps;
        double val1 = val0*spt;
        double val2 = val1*h;
        double val3 = val2/(1024*1024);

        S 1 Reply Last reply
        0
        • V VCProgrammer

          Hi all, I am converting a double value to CString using format but it is giving negative value,

          double val3 = val2/(1024*1024);
          CString test_size;
          test_size.Format(_T("%d"),val3);
          AfxMessageBox(test_size);

          Can anybody please tell me what is the error. i am coding in vc2008

          Y Offline
          Y Offline
          YoungJin Shin
          wrote on last edited by
          #4

          Double and integer are represented as a different binary format. You should casting double to integer before passing a parameter. test_size.Format(_T("%d"),(int) val3);

          1 Reply Last reply
          0
          • R Rajesh R Subramanian

            What is val2?

            It is a crappy thing, but it's life -^ Carlo Pallini

            S Offline
            S Offline
            SnaKeBeD
            wrote on last edited by
            #5

            For answering ur question please tell what is val2 and y u r using "%d"

            ||SnAkeBed||

            R 1 Reply Last reply
            0
            • V VCProgrammer

              double

              double val0 = c*bps;
              double val1 = val0*spt;
              double val2 = val1*h;
              double val3 = val2/(1024*1024);

              S Offline
              S Offline
              SnaKeBeD
              wrote on last edited by
              #6

              the answer depends on val2 ,so debug and see whats tha value coming for val2. use breakpoints.

              ||SnAkeBed||

              1 Reply Last reply
              0
              • V VCProgrammer

                Hi all, I am converting a double value to CString using format but it is giving negative value,

                double val3 = val2/(1024*1024);
                CString test_size;
                test_size.Format(_T("%d"),val3);
                AfxMessageBox(test_size);

                Can anybody please tell me what is the error. i am coding in vc2008

                M Offline
                M Offline
                Malli_S
                wrote on last edited by
                #7

                You are trying tackling with variable of type double variable. If you really wanna to cast the double into integer use casting as :

                test_size.Format(_T("%d"),(int) val3);

                or use proper format specifier :

                test_size.Format(_T("%f"), val3);

                -Malli...! :rose:****

                1 Reply Last reply
                0
                • S SnaKeBeD

                  For answering ur question please tell what is val2 and y u r using "%d"

                  ||SnAkeBed||

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #8

                  SnaKeBeD wrote:

                  For answering ur question please tell what is val2 and y u r using "%d"

                  My question itself was, what is "val2". May be you wanted to reply to the OP?

                  It is a crappy thing, but it's life -^ Carlo Pallini

                  1 Reply Last reply
                  0
                  • V VCProgrammer

                    Hi all, I am converting a double value to CString using format but it is giving negative value,

                    double val3 = val2/(1024*1024);
                    CString test_size;
                    test_size.Format(_T("%d"),val3);
                    AfxMessageBox(test_size);

                    Can anybody please tell me what is the error. i am coding in vc2008

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

                    VCProgrammer wrote:

                    Can anybody please tell me what is the error.

                    Yes: you haven't properly read the documentation about format specification fields, see for instance [^]. :)

                    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.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    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