By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

Creating Blank if zero decimal format string

P: n/a
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;;#"));
}
}
Aug 30 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Sat, 30 Aug 2008 06:39:46 -0700, Andrus <ko********@hot.eewrote:
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.
That's because 'f' isn't a valid character in custom numeric format
strings.

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

You can use the standard numeric format strings or you can write a custom
numeric format string. You can't mix and match.

Pete
Aug 30 '08 #2

P: n/a
That's because 'f' isn't a valid character in custom numeric format
strings.

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
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.

Aug 30 '08 #3

P: n/a
On Sat, 30 Aug 2008 10:04:57 -0700, Andrus <ko********@hot.eewrote:
[...]
Custom format strings discard leading zero if # format char is used or
show zeroes always if 9 format char is used.
'9' isn't a valid custom numeric format string character. So, I don't
know what you mean here.
Only standard format string which produces desired output seems top be f
format.
However f format shows zero instead of blank.
The custom numeric format "0.00" is one custom format string that is
essentially equivalent to "F".
How to create format string which is like standard f format string but
shows blank if value is zero ?
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".

Pete
Aug 30 '08 #4

P: n/a
'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
>How to create format string which is like standard f format string but
shows blank if value is zero ?

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.

Aug 31 '08 #5

P: n/a
On Sun, 31 Aug 2008 08:54:33 -0700, Andrus <ko********@hot.eewrote:
[...]
>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.
No, it doesn't. If you don't specify a precision, it uses the current
culture's NumberFormatInfo.NumberDecimalDigits property. All you have to
do to match the behavior as "F" is to use the same number of digits in
your custom format as the NumberDecimalDigits property indicates.

On my installation (culture of US English), the default precision is 2,
and that's how many decimal places "F" shows, even if there are no
significant digits to the right of the decimal.
[...]
>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.
For the US English culture, so does "F". Your own culture settings might
be different. But "F" does not take into account the number itself when
deciding how many places to show.
I want custom format which is same as F
Then just write a format like "0.00", except with as many decimal digits
as is appropraite for the culture you're using. If you want to do this
dynamically, you could use something like this:

int cdigitDecimal =
CultureInfo.CurrentCulture.NumberFormat.NumberDeci malDigits;
string strDecimalFormat = "{1:0." +
new String('0', cdigitDecimal) + "}";

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

Pete
Aug 31 '08 #6

P: n/a
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.

Aug 31 '08 #7

P: n/a
On Sun, 31 Aug 2008 13:28:11 -0700, Andrus <ko********@hot.eewrote:
[...]
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
So, you want the behavior of the "G" code, not the "F" code.

As far as I know, there are no custom formatting codes that do that. I
would not disagree if you felt this was an oversight in .NET.
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.
What about just writing a method that returns the formatted string you
want? Does this have to be a formatting code?

Pete
Aug 31 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.