最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Format number to a string such that spaces are inserted rather than 0s - Stack Overflow

programmeradmin3浏览0评论

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
  • 2 I don't think that's gonna be possible with the built-in number formatter even with a custom pattern. But you probably need to create that very specific format yourself. What happens for a number like 1234567? What happens if the number has decimal places? – derpirscher Commented Mar 6 at 23:19
  • 1 -,300 - what country are you from? – mjwills Commented Mar 7 at 3:20
  • 3 var s = string.Format( "{0,1:+;-}{0,6:#\\,000;#\\,000}", 1236 ); – Gerry Schmitz Commented Mar 7 at 3:46
  • 1 @mjwills if you're asking because you're confused why in the world I want to format a number to display like this, it's because I'm attempting to replicate the format for numeric input into an aircraft display which uses this absurd looking format. I'm assuming it does this because it inherently communicates meta inforamtion to the pilot about what the computer is expecting to recieve (i.e., that the data inputted may reasonbly exceed one thousand such as altitude information vs an input which would not exceed one thousand such as a heading) – the_pied_shadow Commented Mar 7 at 14:22
  • @GerrySchmitz this seems to be doing what I need and I've learned something about formating as well! – the_pied_shadow Commented Mar 7 at 14:24
Add a comment  | 

1 Answer 1

Reset to default 0

I 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.

发布评论

评论列表(0)

  1. 暂无评论