When you convert some number to a string and pass in a format such as "00", it will insert 0's if the number doesn't have digits filling that spot. Example:
1.ToString("00") -> "01"
I want this kind of functionality but where a space is used rather than a 0 if a digit doesn't fill it. With the above example that would look like
1.ToString("unknownFormat") -> " 1"
Simply padding can't solve my specific use case. This is because I need to have characters which come both after AND before these spaces. Here's an example of what I'm doing right now, it's result, and what I want to produce:
Here's my current format:
format = "+##\,000;-##\,000"
Here's what it currently produce with various numbers:
25.ToString(format) -> "+,025"
-300.ToString(format) -> "-,300"
1236.ToString(format) -> "+1,236"
Here's what I want those numbers to produce instead:
25.ToString(format) -> "+ ,025"
-300.ToString(format) -> "- ,300"
1236.ToString(format) -> "+ 1,236"
This is why padding and similar solutions don't work in my use case. As far as I can tell this simply isn't possible with string formatting. But that suprises me and this isn't an area I'm exprienced in so I'm hoping I'm wrong and someone here knows the magic sauce. Thanks for you time either way!
When you convert some number to a string and pass in a format such as "00", it will insert 0's if the number doesn't have digits filling that spot. Example:
1.ToString("00") -> "01"
I want this kind of functionality but where a space is used rather than a 0 if a digit doesn't fill it. With the above example that would look like
1.ToString("unknownFormat") -> " 1"
Simply padding can't solve my specific use case. This is because I need to have characters which come both after AND before these spaces. Here's an example of what I'm doing right now, it's result, and what I want to produce:
Here's my current format:
format = "+##\,000;-##\,000"
Here's what it currently produce with various numbers:
25.ToString(format) -> "+,025"
-300.ToString(format) -> "-,300"
1236.ToString(format) -> "+1,236"
Here's what I want those numbers to produce instead:
25.ToString(format) -> "+ ,025"
-300.ToString(format) -> "- ,300"
1236.ToString(format) -> "+ 1,236"
This is why padding and similar solutions don't work in my use case. As far as I can tell this simply isn't possible with string formatting. But that suprises me and this isn't an area I'm exprienced in so I'm hoping I'm wrong and someone here knows the magic sauce. Thanks for you time either way!
Share Improve this question asked Mar 6 at 22:48 the_pied_shadowthe_pied_shadow 4051 gold badge5 silver badges14 bronze badges 5 |1 Answer
Reset to default 0I don't know if the formatting below is exactly what you're looking for. But if you want to write, for example, a 2 digits number, padding with spaces the missing digits on the left, here is the solution:
string s1 = string.Format("{0,2}", 1);// -> " 1"
string s2 = string.Format("{0,2}", 10);// -> "10"
Using string interpolation, you can easily add characters before or after the string:
int x = 1;
string s1 = $"{x,2}";// -> " 1" (s1.Length == 2)
string s2 = $"{x,4}";// -> " 1" (s2.Length == 4)
double y = 12.67;
string s3 = $"{y,7:#.000}";// -> " 12.670" (s3.Length == 7)
string s4 = $"{y,8:#.000}";// -> " 12.670" (s4.Length == 8)
int x1 = 1;
double y1 = -12.67;
string str1 = $"{((x1 >= 0) ? "+" : "-")}{Math.Abs(x1),8:#.000}";// -> "+ 1.000"
string str2 = $"{((y1 >= 0) ? "+" : "-")}{Math.Abs(y1),8:#.000}";// -> "- 12.670"
Using extention methods you get something more elegant:
public static class Extensions
{
public static string ToStringExt(this int value) => $"{((value >= 0) ? "+" : "-")}{Math.Abs(value),8:#.000}";
public static string ToStringExt(this double value) => $"{((value >= 0) ? "+" : "-")}{Math.Abs(value),8:#.000}";
public static string ToStringWithLeadingZero(this double value) => $"{((value >= 0) ? "+" : "-")}{Math.Abs(value),8:0.000}";
}
....
....
1.ToStringExt(); // -> "+ 1.000"
(-0.12).ToStringExt(); // -> "- .120"
(-0.57).ToStringWithLeadingZero(); // -> "- 0.570"
With string interpolation it is possibile to get something close to what you need. I guess with a single format string to use with ToString() it isn't possible.
-,300
- what country are you from? – mjwills Commented Mar 7 at 3:20