I have int and nullable int variables that I want to convert to string using a specific culture format, namely to separate the number with dots.
var culture = CultureInfo.CreateSpecificCulture("de-DE");
int contactSum = 123456;
int? resultSum = 654321;
For the int I can do the following:
Console.WriteLine($"The contact sum was: {contactSum.ToString("N0", culture)}");
This will output the following
The contact sum was: 123.456
Nullable int however has no overloads for formatting. How can I format the nullable int the same way as the int? Or is there a better way to do this type of formatting?
The end result would be the following:
The result sum was: 654.321
Here are the two different ways you could achieve your goal.
var culture = CultureInfo.CreateSpecificCulture("de-DE");
int contactSum = 123456;
int? resultSum = 654321;
Console.WriteLine($"The contact sum was: {contactSum.ToString("N0", culture)}");
Console.WriteLine($"The contact sum was: {resultSum?.ToString("N0", culture)}");
Console.WriteLine($"The contact sum was: {(resultSum.HasValue ? resultSum.Value.ToString("N0", culture) : "NULL")}");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With