Help me with formatted int32 strings. [modified]
-
We can use int32.TryPrase("1231312",out tmpInt32Value) for normal situtations. But the following code, string outputString = (12345).ToString("ABCDE-0000000000"); I do not have a good idea to read such string ABCDE-0000012345 :(( Especially when the format string is "ABCDE-0066000880"
modified on Tuesday, May 26, 2009 3:09 AM
-
We can use int32.TryPrase("1231312",out tmpInt32Value) for normal situtations. But the following code, string outputString = (12345).ToString("ABCDE-0000000000"); I do not have a good idea to read such string ABCDE-0000012345 :(( Especially when the format string is "ABCDE-0066000880"
modified on Tuesday, May 26, 2009 3:09 AM
-
Thank you.:) I do not have a good idea to convert string ABCDE-0000012345 to int32 :(( Especially when the format string is "ABCDE-0066000880"
-
Thank you.:) I do not have a good idea to convert string ABCDE-0000012345 to int32 :(( Especially when the format string is "ABCDE-0066000880"
-
We can use int32.TryPrase("1231312",out tmpInt32Value) for normal situtations. But the following code, string outputString = (12345).ToString("ABCDE-0000000000"); I do not have a good idea to read such string ABCDE-0000012345 :(( Especially when the format string is "ABCDE-0066000880"
modified on Tuesday, May 26, 2009 3:09 AM
Your going to format your interger to a predefine format. Example 1
string outputString = (12345).ToString("ABCDE-0000000000"); //Output: "ABCDE0000000000"
Example 2string outputString = (12345).ToString("ABCDED-0000000000"); //Output: "ABCDED-0000012345"
If your are expecting that the output of Example 1 is ABCDE-0000012345 you are wrong because "E-0" means Scientific notation. Note: If any of the strings "E", "E+", "E-", "e", "e+", or "e-" are present in the format string and are followed immediately by at least one '0' character, then the number is formatted using scientific notation with an 'E' or 'e' inserted between the number and the exponent. The number of '0' characters following the scientific notation indicator determines the minimum number of digits to output for the exponent. The "E+" and "e+" formats indicate that a sign character (plus or minus) should always precede the exponent. The "E", "E-", "e", or "e-" formats indicate that a sign character should only precede negative exponents. See Custom Numeric Format String[^ for more details. Happing Coding... -
What kind of strings are that ? Do you mean : ABCDE-0000012345 = 12345 ABCDE-0066000880 = 66000880 ???
when the format string is "ABCDE-000066000880"; int 1234 custom formated value is "ABCDE000066123884" My question is how to get the int value by custom format string "ABCDE-000066000880" and formatted string "ABCDE000066123884". :)
-
Your going to format your interger to a predefine format. Example 1
string outputString = (12345).ToString("ABCDE-0000000000"); //Output: "ABCDE0000000000"
Example 2string outputString = (12345).ToString("ABCDED-0000000000"); //Output: "ABCDED-0000012345"
If your are expecting that the output of Example 1 is ABCDE-0000012345 you are wrong because "E-0" means Scientific notation. Note: If any of the strings "E", "E+", "E-", "e", "e+", or "e-" are present in the format string and are followed immediately by at least one '0' character, then the number is formatted using scientific notation with an 'E' or 'e' inserted between the number and the exponent. The number of '0' characters following the scientific notation indicator determines the minimum number of digits to output for the exponent. The "E+" and "e+" formats indicate that a sign character (plus or minus) should always precede the exponent. The "E", "E-", "e", or "e-" formats indicate that a sign character should only precede negative exponents. See Custom Numeric Format String[^ for more details. Happing Coding...Thank you for your answer. And ,i made a bad formatted sample. Sorry to everyone. we know that code
(1234).ToString("ABCD-000066000880")
's value is ABCD-000066123884. And I want to get a int value from string "ABCD-000066123884" and formatted string "ABCD-000066000880"。 -
Thank you for your answer. And ,i made a bad formatted sample. Sorry to everyone. we know that code
(1234).ToString("ABCD-000066000880")
's value is ABCD-000066123884. And I want to get a int value from string "ABCD-000066123884" and formatted string "ABCD-000066000880"。if your string format is constant to have a format like this "ABCD-0000000000", try to split your string to string array then get the second index value and lastly do your Int32.TryParse thing.
string[] myString = ("ABCD-000066123884").Split('-'); Int32.TryParse(myString[1],out intVar);
Happy Coding... -
if your string format is constant to have a format like this "ABCD-0000000000", try to split your string to string array then get the second index value and lastly do your Int32.TryParse thing.
string[] myString = ("ABCD-000066123884").Split('-'); Int32.TryParse(myString[1],out intVar);
Happy Coding... -
I think I know what he wants : That means : ABCD-000066123884 is not 66123884, but 1234. If that is the problem, then you must write your own method to parse the string.
Yes , It is the problem. :((
-
Yes , It is the problem. :((
Probably this is what you need:
static int MyOwnParser(string text, string formatter)
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < formatter.Length; i++ )
{
if(formatter[i] == '0')
{
stringBuilder.Append(text[i]);
}
}return int.Parse(stringBuilder.ToString());
} -
Probably this is what you need:
static int MyOwnParser(string text, string formatter)
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < formatter.Length; i++ )
{
if(formatter[i] == '0')
{
stringBuilder.Append(text[i]);
}
}return int.Parse(stringBuilder.ToString());
}I was thinking in the same direction, only without the StringBuilder as intermediate result...
int result = 0;
for (int i = 0; i < formatter.Length; i++) {
if (formatter[i] == '0') {
result = result * 10 + (text[i] - '0');
}
}Despite everything, the person most likely to be fooling you next is yourself.
-
I was thinking in the same direction, only without the StringBuilder as intermediate result...
int result = 0;
for (int i = 0; i < formatter.Length; i++) {
if (formatter[i] == '0') {
result = result * 10 + (text[i] - '0');
}
}Despite everything, the person most likely to be fooling you next is yourself.
This method of int32 convert to string with format,maybe can not reverse. When the format string contains "#". From MSDN: # Digit placeholder If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string. Note that this specifier never displays the '0' character if it is not a significant digit, even if '0' is the only digit in the string. It will display the '0' character if it is a significant digit in the number being displayed. The "##" format string causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "##" would result in the value 35.