Formatting Numbers

I want to convert some decimal and floating point numbers to strings.  I have been looking for examples of how to do this and came across the following posted example.  It seems to be what I want, i.e., String^ out = i.ToString...  However, when I run this example I get '02' on the screen, where I would like to see '05'.  What am I missing

// example from another question  //

using namespace System;

int main()
{
   int i = 5;
   Console::WriteLine(i.ToString()->Format("02"));
}


Answer this question

Formatting Numbers

  • Ashvin

    Hi,

    Thanks Ayman Shoukry for moving this. Its your format string that causes the problem here. Try this instead:

    using namespace System;

    int main()
    {
       int i = 5;
       Console::WriteLine(i.ToString()->Format("00"));
    }




    cheers,


    Paul June A. Domag



  • BasildonBond

    Paul,

    I tried going thru the referenced material, but I didn't really find what I wanted.  I never saw anything similar to your use and I did't see any clear and simple example such as seems to exist with printf in the old books I have around.  Can you or anyone else suggest a better, simpler reference

    Gordon.

  • brijesh patel

    Hi,

    Try this instead:

    #include "stdafx.h"

    using namespace System;

    int main(array<System::String ^> ^args)
    {
        int i = 5;
        Console::WriteLine(i.ToString("00"));
        Console::WriteLine(L"Hello World");
        return 0;
    }



  • Neil Hewitt

    Hi,

    Here's a documentation on how to use the Format method of string class.






    cheers,

    Paul June A. Domag


  • Dannil

    Paul,

    Thanks.  Your change  worked.  Where can I find the list of how Format parameters work

    Gordon.

  • TheHighKing

    Hi,

    Here's a good reference for numeric formats. And here ar the formats being used by datetime.





    cheers,

    Paul June A. Domag


  • Wa-gee

    Here is the complete progam I ran (with a break at return so I could see the results).  Here I got '00", not '05'  So, what am I doing wrong    And, where do I find some good examples of what goes in the parameter list for Format

    Gordon.

    #include "stdafx.h"

    using namespace System;

    int main(array<System::String ^> ^args)
    {
        int i = 5;
        Console::WriteLine(i.ToString()->Format("00"));
        Console::WriteLine(L"Hello World");
        return 0;
    }





  • present

                   Thanks.
  • sireesha

    Moving to the .NET development forums since folks there should have more knowledge about the related issues.

    Thanks,
      Ayman Shoukry
      VC++ Team



  • Formatting Numbers