Johannes Schacht
Johannes Schacht

Reputation: 1354

Formatting BigInteger leads empty for 0

I am formatting a BigInteger value of 0 and get an empty string as a result. Is that the expected behavior?

System.Numerics.BigInteger value = 0;
string xx = value.ToString("#", System.Globalization.CultureInfo.InvariantCulture);

xx is string.Empty after these two statements. If I set value to 10 I'm getting "10".

Upvotes: 2

Views: 43

Answers (2)

Equalsk
Equalsk

Reputation: 8214

See the documentation for the "#" custom specifier:

https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx#SpecifierD

Note that this specifier never displays a zero that is not a significant digit, even if zero is the only digit in the string. It will display zero only if it is a significant digit in the number that is being displayed.

In your case 0 is not a significant digit and is therefore not displayed.

Use 0 as the specifier instead.

Upvotes: 2

p.s.w.g
p.s.w.g

Reputation: 149050

Yes, that's the expected behavior. From MSDN:

If the value that is being formatted has a digit in the position where the "#" symbol appears in the format string, that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string.

Emphasis mine

You can use 0 as a format string instead if you always want at least one digit:

string xx = value.ToString("0", System.Globalization.CultureInfo.InvariantCulture);

Yields:

  • 0"0"
  • 10"10"

Upvotes: 5

Related Questions