472,958 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Creating Blank if zero decimal format string

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
7 13484
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
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
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
'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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: pelcovits | last post by:
How do I get Access to put a blank in a report column (but not eliminate the record completely), when the field in question is currency format, and the value is zero? My understanding is that null...
5
by: Tom | last post by:
How can I find the first blank control on a form by looking at the controls in the order of their tab order? Thanks! Tom
1
by: Dixie | last post by:
I wish to add some fields to an existing table in code. I am using the following code from rkc. CurrentDb.Execute ("ALTER TABLE MyTable ADD MyNewField Text 25") This works , but I need to also set...
2
by: Todd_M | last post by:
I was wondering what anyone might suggest as "best practice" patterns for streaming out fixed formatted text files with C#? Let's say we get our data in a dataset table and we need to iterate over...
33
by: gk245 | last post by:
I mean, anything that follows a 0 is automatically turned into a octal number. I want to have a integer variable that will hold numbers as integers even if they begin with a zero. for example:...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
5
by: sara | last post by:
I've looked at all the posts I could and don't see the solution. I am aware that the format to get a BLANK when the number is 0 is: $ #,###.00;($ #,###.00);"" But I can't, for the life of me,...
8
by: Andrew Poulos | last post by:
In my limited testing with FF 2, IE 6 and Opera 9 when I divided a positive integer, that is less than 100, by 100 I get a leading zero in front of the decimal point. For example 80/100 gives...
3
by: hharry | last post by:
Hello All, I am using the following to format numbers precise to 2 decimal places with thousand separators: Double number = 100000.87; string MyString = String.Format("{0:0,0.00}", number);...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.