Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
String interpolation with boolean formatting

How can I specify a format string for a boolean that's consistent with the other format strings for other types?

Given the following code:

double d = Math.PI;
DateTime now = DateTime.Now;
bool isPartyTime = true;

string result = $"{d:0.0}, {now:HH:mm}, time to party? {isPartyTime}";

I can specify a format for every primitive type, except for bool it seems. I know I can do:

string result = $"{d:0.0}, {now:HH:mm}, time to party? {(isPartyTime ? "yes!" : "no")}";

However this is still inconsistent with the other types.

Is there a way of formatting booleans in interpolated strings that is consistent?

P.S. I did search for an answer including this link:

https://stackoverflow.com/questions/tagged/c%23+string-interpolation+boolean

And surprisingly had zero results.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Unfortunately, no, there isn't.

According to Microsoft, the only data types with format strings are:

  • Date and time types (DateTime, DateTimeOffset)
  • Enumeration types (all types derived from System.Enum)
  • Numeric types (BigInteger, Byte, Decimal, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32, UInt64)
  • Guid
  • TimeSpan

Boolean.ToString() can only return "True" or "False". It even says, if you need to write it to XML, you need to manually perform ToLowerCase() (from the lack of string formatting).

over 4 years ago · Santiago Trujillo Report

0

This may be obvious but to cut down the repetition, you could always create an extension method. It gets you half way there at least.

public static class MyExtensions
{
    public static string ToYesNo(this bool boolValue)
    {
        return boolValue ? "Yes" : "No";
    }
}

static void Main(string[] args)
{
    var booleanValue = true;

    Console.WriteLine(booleanValue.ToYesNo());
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!