Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nullable int Culture Specific toString()

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

like image 533
yno Avatar asked Oct 19 '25 09:10

yno


1 Answers

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")}");
like image 184
Karthikeyani Anandhan Avatar answered Oct 21 '25 22:10

Karthikeyani Anandhan