Skip to content

Different output for a number, a string, an empty string and null #344

Answered by axunonb
sygmond asked this question in Q&A
Discussion options

You must be logged in to vote

The issue as described does not go back to the Parser, but to the IFormatters.

The format string in the example will auto-select the formatter (implicitly).
So the answer the question above is still relevant.
In your case, it looks like the PluralLocalizationFormatter kicks in, which leads to the results as described.
If anything in the output is not as expected, it's always recommended to to use a named formatter.

In order to distinguish null, string.Empty, or a another value, the best way is:

// Explicitly let the ChooseFormatter do the job
Smart.Format("{0:choose(null|):null|empty|{}}", 1234); 
// Result: "1234"
Smart.Format("{0:choose(null|):null|empty|{}}", "1234"); 
// Result: "1234"

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@axunonb
Comment options

Comment options

You must be logged in to vote
1 reply
@sygmond
Comment options

Answer selected by axunonb
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
2 participants
Converted from issue

This discussion was converted from issue #343 on July 31, 2023 16:03.