Hi I just want to ask about how can i add padding on string.Format so that when I show it , the mask is applied with leading zeros
Heres my c# code
Model.Phone = String.Format("{0:(###) ###-####}", double.Parse(#e.Phone));
Expected result should be
(012) 345-6789
But the results I am getting is
(12) 345-6789
and the leading zero is missing, Hope someone can help me in this problem , Thanks
You would use 000 instead of ###, read more about format in MSDN article Custom Numeric Format Strings
String.Format("{0:(000) ###-####}", double.Parse(#e.Phone));
Format specifier "0"
Replaces the zero with the corresponding digit if one is present;
otherwise, zero appears in the result string.
Format specifier "#"
Replaces the "#" symbol with the corresponding digit if one is
present; otherwise, no digit appears in the result string.
Related
i want to format a string. Assume there is:
string unformatedString="004897582515"
string stringFormater="{0:00#-###-###-####}"
After formatting:
string result = String.Format(stringFormater, Int64.Parse(unformatedString));
the result is: 000-044-788-9556
I'd like to know why? Because after parsing unformatedString into Int64 I am getting 4897582515 value as integer, but after formatting it there are always additional zeros(it's based on count of zeros in the beginning of unformatedString).
The simple reason 004897582515 is turning into 000-0xx-xxx-xxx with the format specifier "{0:00#-###-###-####}" is because of the 0's at the beginning
Custom numeric format strings
Format specifiers
0 : Zero placeholder
Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
Maybe you want "{0:###-###-###-####}"
Format specifiers
# : Digit placeholder
Replaces the # symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.
Which in worst case will result in "-xxx-xxx-xxxx"
However, you could hack in a TrimStart('-')
Console.WriteLine(string.Format("{0:###-###-###-####}", 004897582515).TrimStart('-'));
Which will remove any leading dash
Output
489-758-2515
Full Demo Here
This question already has answers here:
How to use string.Format() to format a hex number surrounded by curly brackets?
(4 answers)
Closed 5 years ago.
Now, here is a strange problem I have with String.Format while formatting zero as a hex string. Please take a look at the following example:
public override string ToString()
{
return $"{{{LowPart:X}-{HighPart.ToString("X")}}}";
}
Above code works alright with HighPart being zero, but the following two give me a wrong result:
public override string ToString()
{
return $"{{{LowPart:X}-{HighPart:X}}}";
}
public override string ToString()
{
return string.Format("{{{0:X}-{1:X}}}", LowPart, HighPart);
}
With LowPart being 50 for example and HighPart being 0, both of these will return "{32-X}" instead of "{32-0}".
I don't know the "Why" that this happens. I also tried googling it and found no answer. So here I am to see if anyone have a clue about this.
BTW, I am using VS2015 and .Net4.5
Edit: Turns out that the problem is with the ending }} in the string. Still strange to me though.
Seems like a bug in formatting engine as if I add a space there in the middle, it will work. Like: $"{{{LowPart:X}-{HighPart:X} }}"
Console.WriteLine(string.Format("{0:X}", 50));
returns 32. 50 in decimal is 32 in hexadecimal, because 2*16^0+3*16^1 = 2 + 48 = 50
Edit after understanding what the question is about:
I believe this page holds the answer (https://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx)
The way escaped braces are interpreted can lead to unexpected results.
For example, consider the format item "{{{0:D}}}", which is intended
to display an opening brace, a numeric value formatted as a decimal
number, and a closing brace. However, the format item is actually
interpreted in the following manner:
The first two opening braces ("{{") are escaped and yield one opening
brace.
The next three characters ("{0:") are interpreted as the start of a
format item.
The next character ("D") would be interpreted as the Decimal standard
numeric format specifier, but the next two escaped braces ("}}") yield
a single brace. Because the resulting string ("D}") is not a standard
numeric format specifier, the resulting string is interpreted as a
custom format string that means display the literal string "D}".
The last brace ("}") is interpreted as the end of the format item.
The final result that is displayed is the literal string, "{D}". The
numeric value that was to be formatted is not displayed.
So the "{1:X}}}" is interpreted as "X}", because it tries to apply "X}" which is not recognized. The same happens in String.Format("{0:HELLO}",50)
The credit for explaining the issue goes to Hans Kesting's answer in How to use string.Format() to format a hex number surrounded by curly brackets? I flagged the question as possible duplicate because of this.
I need for this to work in a single format statement and to work for both ints and decimals:
For example:
int myInt=0;
decimal myDecimal=0.0m;
// ... Some other code
string formattedResult1=String.Format("{0}",myInt);
string formattedResult2=String.Format("{0}",myDecimal);
The expected results are:
"" (i.e., string.Empty) if the item to be formatted is zero
and a numeric value (e.g., "123.456" for the decimal version) if it isn't.
I need for this to occur exclusively as a result of the format specification in the format string.
This should do:
string formattedResult1 = string.Format("{0:0.######;-0.######;\"\"}", myInt);
The colon introduces a numeric format string. The numeric format string is divided into 3 parts with semicolons: part 1 is for positive numbers, part 2 for negative numbers, and part 3 for zeros. To define a blank string you need to delimit it with double quotes otherwise it doesn't like it.
See MSDN for the full syntax.
based from the accepted answer above i have done the same thing in microsoft "report builder"
this worked for me (shows 2 decimal places, blank for zero) :
,##0.00;-#,##0.00;""
I would like to represent the number 2.3421 as 2.34 but my current formatting shows it as 02.34
If I had the number 1342.323 I would want to show this as 1,342.32
If I had 0.23 this would be shown as 0.23.
What do I change my format string to achieve this? I have:
"{0:0,0.00}"
Use # where a number is optional instead of 0:
"{0:#,0.00}"
See Custom Numeric Format Strings on MSDN:
"#" | Digit placeholder
Replaces the pound sign with the corresponding digit if one is present; otherwise, no digit appears in the result string.
Try this:
{0:#,##0.00}
1342.323 should then be 1,342.32
From my previous question, Converting chinese character to Unicode, I had a good answer but with some code I didn't understand:
Console.WriteLine("U+{0:x4}", (int)myChar);
Could anyone explain this?
Console.WriteLine("U+{0:x4}", (int)myChar);
is the equivalent to the call:
Console.WriteLine("U+{0}", ((int)myChar).ToString("x4"));
In a format string, the : indicates that the item should be displayed using the provided format. The x4 part indicates that the integer should be printed in its hexadecimal form using 4 characters. Refer to standard numeric format strings for more information.
The 0 indicates which positional argument to substitute. The x displays a hexadecimal number, the 4 has it display four digits.
For example, the character ΘΏ (LATIN SMALL LETTER S WITH SWASH TAIL, codepoint 575) is printed as U+023F since 57510 = 23F16.
That will simply create the literal string "U+1234"... now if you are wanting to convert a unicode code point into a char, you want Convert.ToChar(myChar)
http://msdn.microsoft.com/en-us/library/3hkfdkcx.aspx