Creating Blank if zero decimal format string

A

Andrus

How to create format string for decimal data type
which shows blank for zero and default format otherwize ?

I tried format string "f;f;#" but this shows f for nonzero numbers.

Andrus.


using System.Windows.Forms;

class AppMainEntry
{
static void Main()
{
// Expected: 0.40 actual: f
MessageBox.Show((0.40m).ToString("f;;#"));

// expected: 123.40 actual: f
MessageBox.Show((123.40m).ToString("f;;#"));

// expected: 123.4 actual: f
MessageBox.Show((123.4m).ToString("f;;#"));

// OK: expected: empty actual: empty
MessageBox.Show((0m).ToString("f;;#"));
}
}
 
A

Andrus

That's because 'f' isn't a valid character in custom numeric format

Yes, I have read this page several times.
You can use the standard numeric format strings or you can write a custom
numeric format string. You can't mix and match.

Sure.
Custom format strings discard leading zero if # format char is used or show
zeroes always if 9 format char is used.
Only standard format string which produces desired output seems top be f
format.
However f format shows zero instead of blank.

How to create format string which is like standard f format string but shows
blank if value is zero ?

Andrus.
 
A

Andrus

'9' isn't a valid custom numeric format string character. So, I don't
know what you mean here.

I'm sorry I meant '0'
The custom numeric format "0.00" is one custom format string that is
essentially equivalent to "F".

0.00 shows always 2 places after comma
F format shows as many places after comma as decimal contains.
So I don't understand how this is same as F
Use a valid custom numeric format string that correctly describes your
desired output, instead of the character 'f' or 'F'. It being a custom
numeric format, you can use whatever you like, as long as it's valid. But
you might want to start with "0.00".

0.00 shows always 2 places after comma.
I want custom format which is same as F

I tried

"#" - it displays .45 i.e without leading zero

"0.#;0.#;#" shows 1.1 instead of 1.10 for decimal 1.10m

Which custom format is same as F ?

Andrus.
 
A

Andrus

Then just use that string wherever you need it (for example, in building
the custom format string that handles zero the way you want).

Thank you. I'm sorry my previous letter was wrong, f format cannot used.

I want that result string contains as may decimal places after comma as
decimal value actually contains
*except* zero must be converted to empty string.

ToString() without argument or with empty format string produces this output
except it outputs 0m as 0 :

0.400m is converted to 0.4000
123.40m is converted to 123.40
123.4m is converted to 123.4
0m is converted to 0

How to create format string which works as above except 0m, 0.00m etc is
converted to empty string ?
I tried ";;#" format string but this does not output any digits.

Andrus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top