Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6656217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:36:58+00:00 2026-05-26T01:36:58+00:00

I was wondering if there’s a syntax for formatting NULL values in string.Format, such

  • 0

I was wondering if there’s a syntax for formatting NULL values in string.Format, such as what Excel uses

For example, using Excel I could specify a format value of {0:#,000.00;-#,000.00,NULL}, which means display the numeric value as number format if positive, number format in parenthesis if negative, or NULL if the value is null

string.Format("${0:#,000.00;(#,000.00);NULL}", someNumericValue);

Edit

I’m looking for formatting NULL/Nothing values for all data types, not just numeric ones.

My example is actually incorrect because I mistakenly thought Excel used the 3rd parameter if the value was NULL, but it’s actually used when the value is 0. I’m leaving it in there because it’s the closest thing I can think of to what I was hoping to do.

I am hoping to avoid the null coalescing operator because I am writing log records, and the data is not usually a string

It would be much easier to write something like

Log(string.Format("Value1 changes from {0:NULL} to {1:NULL}", 
    new object[] { oldObject.SomeValue, newObject.SomeValue }));

than to write

var old = (oldObject.SomeValue == null ? "null" : oldObject.SomeValue.ToString());
var new = (newObject.SomeValue == null ? "null" : newObject.SomeValue.ToString());

Log(string.Format("Value1 changes from {0} to {1}", 
    new object[] { old, new }));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:36:59+00:00Added an answer on May 26, 2026 at 1:36 am

    You can define a custom formatter that returns "NULL" if the value is null and otherwise the default formatted string, e.g.:

    foreach (var value in new[] { 123456.78m, -123456.78m, 0m, (decimal?)null })
    {
        string result = string.Format(
            new NullFormat(), "${0:#,000.00;(#,000.00);ZERO}", value);
        Console.WriteLine(result);
    }
    

    Output:

    $123.456,78
    $(123.456,78)
    $ZERO
    $NULL
    

    Custom Formatter:

    public class NullFormat : IFormatProvider, ICustomFormatter
    {
        public object GetFormat(Type service)
        {
            if (service == typeof(ICustomFormatter))
            {
                return this;
            }
            else
            {
                return null;
            }
        }
    
        public string Format(string format, object arg, IFormatProvider provider)
        {
            if (arg == null)
            {
                return "NULL";
            }
            IFormattable formattable = arg as IFormattable;
            if (formattable != null)
            {
                return formattable.ToString(format, provider);
            }
            return arg.ToString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering if there is a way I could trigger an event, so that any
Wondering if there is a fast way, maybe with linq?, to convert a Dictionary<string,string>
Wondering if there is some preference I can set in excel so that when
Just wondering if there was a way i could print out a number to
Wondering if there is any Text to Speech software available as a plug in
Wondering if there's any not-too-hard way to edit non-form text in html 4. I
Wondering if there is any way to get the lambda expressions that result from
Wondering if there is a good way to generate temporary URLs that expire in
Just wondering if there is an easy way to add the functionality to duplicate
Just wondering if there is any way (in C) to get the contents of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.