none
Convert String to Integer in c#

    Question

  •  

    string str = "0123";

    int a = Convert.ToInt32(str);

     

    Console.WriteLine(a);

     

    Console.Read();

     

    The above code converts string '0123' to integer , but while printing the converted integer its displaying '123' the '0' is missing..

     

    Give ur suggestions!!!!


    Regards, Antony Software Engineer
    Friday, June 04, 2010 5:43 AM

Answers


  •             string str = "0123";
                int a = Convert.ToInt32(str);
                Console.WriteLine(a.ToString("0000"));
                Console.Read();



    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:12 AM
    Friday, June 04, 2010 6:14 PM
  • http://msdn.microsoft.com/en-us/library/dd260048.aspx
    Amit Bansal http://www.oops4you.blogspot.com/
    • Proposed as answer by danych Friday, June 04, 2010 7:51 AM
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:10 AM
    Friday, June 04, 2010 7:04 AM
  • Hi,

    The convertion from string to int removes the precceding zeros. If you don't have a fixed padding, i suggest hold the string value for priting and use a copy for validation. Then you can print the correct output depending on the validation result.

    Hope this helps.

    Vijay

    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:11 AM
    Friday, June 04, 2010 7:17 AM
  • Validate the converted int, and then display the original string.
    http://blog.voidnish.com
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:11 AM
    Friday, June 04, 2010 3:19 PM
  • As others have said, printing the value of the original string variable would solve your problems, especially since "0123" is not an integer and having a zero in front would do be useless (and harmful!) if used in a mathematical operation. If you do need to use that value inside a mathematical operation convert it to int, just like you did before, and use "123" just for that purpose. The results cant then be converted back into an int for future output. And once its converted back into a string you would be able to pad it, too.

    Remember that when it comes to outputing values, nothing other datatype is more flexible as strings.

    • Edited by GonzoKnight Friday, June 04, 2010 7:11 PM
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:11 AM
    Friday, June 04, 2010 5:20 PM
  • You could simpy convert it to a number and display the original string, that way you don't need to pad it and use whatever value you need for what you want.
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:21 AM
    Friday, June 04, 2010 6:51 PM
  • Hi,

    Thanks for your post.

    Sorry, but I have to say that you can't expect C# to guess your desire. your current code directs C# to print an integer i.e 0123. you expect C# to print '0123' but what will happend if I expect to print '00000123'. so, C# ignore all zeros and every body should pad it as his wish like others cited in previous posts.

    Hope this helps too.

    Regards,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:14 AM
    Friday, June 04, 2010 9:21 PM

All replies

  • You want to output "0123"?
    Friday, June 04, 2010 5:47 AM
  • May I ask why are you converting the string to integer if you want to display it?
    Friday, June 04, 2010 5:53 AM
  • Thanls for ur replies..

    Thomas,  Yes i want the output to be '0123'

    ----------------

    Ahmed....actually i need to convert it  to integer to perform validation..it depends mush on integer value..

    dats y i need to convert it to int.. if i append 0 somewere else other than first place den it swrking fine..

     

     

     


    Regards, Antony Software Engineer
    Friday, June 04, 2010 6:57 AM
  • http://msdn.microsoft.com/en-us/library/dd260048.aspx
    Amit Bansal http://www.oops4you.blogspot.com/
    • Proposed as answer by danych Friday, June 04, 2010 7:51 AM
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:10 AM
    Friday, June 04, 2010 7:04 AM
  • Hi,

    The convertion from string to int removes the precceding zeros. If you don't have a fixed padding, i suggest hold the string value for priting and use a copy for validation. Then you can print the correct output depending on the validation result.

    Hope this helps.

    Vijay

    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:11 AM
    Friday, June 04, 2010 7:17 AM
  • is 9999 the maximum number you will get and number will have leading zeros to make it 4 digit number? Or sometimes you will get 0123 and other times you may recieve 123?

    I am just trying to understand your requirement and the input data to give you an answer that covers all the scenarios you may encounter 

    Friday, June 04, 2010 3:07 PM
  • Validate the converted int, and then display the original string.
    http://blog.voidnish.com
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:11 AM
    Friday, June 04, 2010 3:19 PM
  • As others have said, printing the value of the original string variable would solve your problems, especially since "0123" is not an integer and having a zero in front would do be useless (and harmful!) if used in a mathematical operation. If you do need to use that value inside a mathematical operation convert it to int, just like you did before, and use "123" just for that purpose. The results cant then be converted back into an int for future output. And once its converted back into a string you would be able to pad it, too.

    Remember that when it comes to outputing values, nothing other datatype is more flexible as strings.

    • Edited by GonzoKnight Friday, June 04, 2010 7:11 PM
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:11 AM
    Friday, June 04, 2010 5:20 PM

  •             string str = "0123";
                int a = Convert.ToInt32(str);
                Console.WriteLine(a.ToString("0000"));
                Console.Read();



    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:12 AM
    Friday, June 04, 2010 6:14 PM
  • You could simpy convert it to a number and display the original string, that way you don't need to pad it and use whatever value you need for what you want.
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:21 AM
    Friday, June 04, 2010 6:51 PM
  • Hi,

    Thanks for your post.

    Sorry, but I have to say that you can't expect C# to guess your desire. your current code directs C# to print an integer i.e 0123. you expect C# to print '0123' but what will happend if I expect to print '00000123'. so, C# ignore all zeros and every body should pad it as his wish like others cited in previous posts.

    Hope this helps too.

    Regards,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    • Marked as answer by Liliane Teng Thursday, June 10, 2010 3:14 AM
    Friday, June 04, 2010 9:21 PM
  • Thanks for all contributing to the developer community.

    I share similar information through the article titled [How to Handle Exception to Convert String to Integer (Int32) with C#], or in portuguese, [Como Tratar Exceção ao Converter String para Número Inteiro (Int32) com C#] available at 

    Friday, January 25, 2013 4:23 AM
  • http://msdn.microsoft.com/en-us/library/dd260048.aspx
    Amit Bansal http://www.oops4you.blogspot.com/
    How to: Pad a Number with Leading Zeros

    You can pad an integer with leading zeros by using the "D" standard numeric format string together with a precision specifier. You can pad both integer and floating-point numbers with leading zeros by using a custom numeric format string. This topic shows how to use both methods to pad a number with leading zeros.


    Friday, January 25, 2013 4:26 AM
  • so why zeros are ignored? if we type: 

    string str = "0000";

    it prints only one zero any one can tell me why other three zero are ignored ????

    Saturday, September 14, 2013 1:47 PM