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.
Printable View
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.
how about:
This might need a bit of tweaking (haven't tested it)Code:int CountSigns(int value)
{
int length = value.ToString().Length;
if(value < 0)
lenght++;
return length;
}
Code:int number = -12345;
int charCount = Convert.ToString(number).Length;
Small enhancement possible...
:)Code:int number = -12345;
int charCount = Convert.ToString(number, CultureInfo.InvariantCulture).Length;
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?