-
February 6th, 2010, 07:38 PM
#1
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.
-
February 6th, 2010, 10:21 PM
#2
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)
-
February 7th, 2010, 01:28 AM
#3
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
-
February 8th, 2010, 03:41 AM
#4
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.
-
February 8th, 2010, 03:45 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|