CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2010
    Posts
    13

    Question Number of signs for an int variable?

    Hello,

    Is there any easy way to find out how many signs an int variable has please?

    Example: 101 has three, -7 has one(two?), 1234 has four.

  2. #2
    Join Date
    Dec 2009
    Posts
    22

    Re: Number of signs for an int variable?

    how about:
    Code:
    int CountSigns(int value)
    {
        int length = value.ToString().Length;
        if(value < 0)
          lenght++;
        
        return length;
    }
    This might need a bit of tweaking (haven't tested it)

  3. #3
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Number of signs for an int variable?

    Code:
    int number = -12345;
    int charCount = Convert.ToString(number).Length;
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Number of signs for an int variable?

    Small enhancement possible...
    Code:
    int number = -12345;
    int charCount = Convert.ToString(number, CultureInfo.InvariantCulture).Length;
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Number of signs for an int variable?

    I wonder whether it is really necessary. can an int have a different format in other cultures? I know a double can have a '.' or a ',' but an int is always the same, isn't it?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured