1

I'm trying to format some doubles properly so that they will display properly for my uses (I'm building a statement in Devexpress, so I'm working with a lot of numbers).

Here are the basic formatting rules I'd like to have happen:

1000.2  -> 1,000.20
1000    -> 1,000
1000.22 -> 1,000.22

Is this possible using string formatting in C#? I've tried the following, but not been able to achieve my goal:

#,#.## - gives me 1,000.2 for the first value
#,#.#0 - gives me 1,000.00 for the second value
#,#.00 - gives me 1,000.00 for the second value

EDIT: Some more information. DevExpress gives me the ability to use string formatting to set up the values after they've been bound to the report. We're doing it at report time (and not at calculation time in the behind the scenes code) because we use the Sum function within the tables that DevExpress offers us. The reason we do THAT is so that we can minimize calls to our database by doing one large pull of data, then using that table over and over again in the statement and filtering based on the restrictions within.

EDIT EDIT: Based on the feedback I've receieved here in the comments, it's not possible to perform the formatting I'd like to do with only providing a string format; I would need to insert some code either when I provide the data to the report (and then remove any and all formatting from the report) and perform all summing functions at the code level (to ensure that the sum values have the expected decimal places), or I would need to accept .00 at the end of, for example, some amount of yen (100 JPY would never be represented as 100.00 JPY, as an example).

This is a bit of an esoteric case, but it's good to know!

6 Answers 6

1

You can use string formatting coupled to a simple if condition. To shorten it's use, you can also make it an Extension method. It can look like this :

public static string FormatConditionnaly(this double input)
{
    return input % 1 == 0 ? input.ToString("#,0") : input.ToString("#,0.00");
}

Basically, if you number does not contain any decimals (the % 1 == 0 check), you format it without decimals. If it fails the check, you add the two zeroes.

It is used like that :

const double flatNumber = 1000;
string result1 = flatNumber.FormatConditionnaly(); //1,000

const double numberWithDecimals = 1000.5;
string result2 = numberWithDecimals.FormatConditionnaly(); //1,000,50
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer; however I was hoping to find a string like the ones I provided as examples to allow me to do it with a single format. It's a great solution, I'm just not able to use it.
1

Bit of a hack but you can give this a try:

s = String.Format("{0:N2}", 1000).Replace(".00", "");

1 Comment

Thank you for your response, however as I said to Pierre, I'm hoping to find a way to do it without code, and just by using the format string.
1

Use the "N" format specifier as the format string when you call ToString(); See here

For example:

int intValue = 123456789;
Console.WriteLine(intValue.ToString("N2", 
    CultureInfo.InvariantCulture).Replace(".00", "");

You can customize group sizes etc. as needed.

5 Comments

This won't work in my case--for example, I want to be able to display .20 when given .2, and nothing at all when presented with a whole number. This forces 2 decimal places at all times, or whatever the amount is that I set it to.
To get rid of the .00 you'd have to add a little hack but other than that my edited code takes care of your examples
Understood! I guess it looks like I'm not going to be able to do it without a bit of a hack, which I'll have to check with my manager on. Thank you for your help!
You could also implement IFormatProvider but I think that would be overkill for this situation
Agreed. I think at this point it's an issue with how we're using the platform, not with string formatting. :)
1

Why don't you format the values before binding to DevExpress control using plain old C# (Assuming you are doing a bind, as you have not given sufficient details.)

In c# the Math.Round() should do the trick.

Example Math.Round(doubleValue,2) where the second parameter is the number of decimal places.

EDIT:

#,##0.00

I Do not have DevExpress controls to test my solution but I did find http://documentation.devexpress.com/#windowsforms/CustomDocument1498 online (not sure if you see it already).

It seems you can use the Number or Currency masks.

Also take a look at the Zero Placeholder under the custom section. based on the description, '0' is filled where the user has not supplied a value.

example: 123.4 --> 123.40

2 Comments

In my situation, it's partially because that's 'how it's always been'. In reality though, you're correct in that I should be applying this formatting when I'm setting up my datatable to bind to the control. Dang return/submit. The expanded reason (which I'll expand on shortly) is that we're using the Sum functions within the DevExpress report, which we need to have formatted within DevExpress, not as part of the code behind the statement.
Thank you, but as I mentioned using .00 at the end forces the value 100 to render as 100.00. This is problematic in the case of non-decimal currencies such as yen (which you would never see represented as 98.00, unless you're looking at the rate between currencies).
1

If the string that you're trying to format is in an XRTableCell of an XtraReport instance, you can handle the BeforePrint event on that cell to format its text. This event is triggered anytime that the report is rendered. Call GetCurrentColumnValue to retrieve the value that you want to format, use any of the code methods from the previous answers that will work for you, and then set that cell's text with your formatted string. Using @dweeberly's answer:

private void OnBeforePrint(object sender, PrintEventArgs e)
{
   object value = this.GetCurrentColumnValue("YourField");
   if (value != null)
   {
       yourCell.Text = String.Format("{0:N2}", value.ToString()).Replace(".00", "");
   }
}

1 Comment

Thank you for your answer--it looks like there isn't a way to do what I'm looking for that doesn't involve replacing the .00 in the case of 100 with some code. I don't have enough reputation to mark your answer as helpful, but it definitely was!
0

Based on the feedback I've receieved here in the comments, it's not possible to perform the formatting I'd like to do with only providing a string format; I would need to insert some code either when I provide the data to the report (and then remove any and all formatting from the report) and perform all summing functions at the code level (to ensure that the sum values have the expected decimal places), or I would need to accept .00 at the end of, for example, some amount of yen (100 JPY would never be represented as 100.00 JPY, as an example).

This is a bit of an esoteric case, but it's good to know!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.