views:

151

answers:

4

Is there a display formatter that will output decimals as these string representations in c# without doing any rounding?

decimal -> string

20 -> 20

20.00 -> 20

20.5 -> 20.5

20.5000 -> 20.5

20.125 -> 20.125

20.12500 -> 20.125

0.000 -> 0

{0.#} will round, and using some Trim type function will not work with a bound numeric column in a grid.

+8  A: 

Do you have a maximum number of decimal places you'll ever need to display? (Your examples have a max of 5).

If so, I would think that formatting with "#0.#####" would do what you want.

    static void Main(string[] args)
    {
        var dList = new decimal[] { 20, 20.00m, 20.5m, 20.5000m, 20.125m, 20.12500m, 0.000m };

        foreach (var d in dList)
            Console.WriteLine(d.ToString("#0.#####"));
    }
Toby
And you can always throw an exorbitant* number of # symbols at the format. *not scientific
Anthony Pegram
+1 - incidentally, you don't need the leading #. "0.#####" works identically. And either way, this does round away from zero (just FYI).
TrueWill
+2  A: 

Extension method:

public static class Extensions
{
    public static string TrimDouble(this string temp)
    {
        var value = temp.IndexOf('.') == -1 ? temp : temp.TrimEnd('.', '0');
        return value == string.Empty ? "0" : value;
    }
}

Example code:

double[] dvalues = {20, 20.00, 20.5, 20.5000, 20.125, 20.125000, 0.000};
foreach (var value in dvalues)
    Console.WriteLine(string.Format("{0} --> {1}", value, value.ToString().TrimDouble()));

Console.WriteLine("==================");

string[] svalues = {"20", "20.00", "20.5", "20.5000", "20.125", "20.125000", "0.000"};
foreach (var value in svalues)
    Console.WriteLine(string.Format("{0} --> {1}", value, value.TrimDouble()));

Output:

20 --> 20
20 --> 20
20,5 --> 20,5
20,5 --> 20,5
20,125 --> 20,125
20,125 --> 20,125
0 --> 0
==================
20 --> 20
20.00 --> 2
20.5 --> 20.5
20.5000 --> 20.5
20.125 --> 20.125
20.125000 --> 20.125
0.000 --> 0
jgauffin
A: 

I don't think it's possible out-of-the-box but a simple method like this should do it

public static string TrimDecimal(decimal value)
{
    string result = value.ToString(System.Globalization.CultureInfo.InvariantCulture);
    if (result.IndexOf('.') == -1)
        return result;

    return result.TrimEnd('0', '.');
}
Tim
will not work with "0.0" =)
jgauffin
+1  A: 

It's quite easy to do out of the box:

Decimal YourValue; //just as example   
String YourString = YourValue.ToString().TrimEnd('0','.');

that will remove all trailing zeros from your Decimal.

The only thing that you need to do is add .ToString().TrimEnd('0','.'); to a decimal variable to convert a Decimal into a String without trailing zeros, like in the example above.

In some regions it should be a .ToString().TrimEnd('0',','); (where they us a comma instead of a point, but you can also add a dot and a comma as parameters to be sure)

(you can also add both as parameters)

Mervin
You may not realize this, but when dealing with a `params` argument, you don't have to construct an array explicitly. So instead of the verbose `TrimEnd("0".ToCharArray())` you could just write `TrimEnd('0')` (note: passed single `char` instead of a `char[]`).
Dan Tao
@Dan Tao: Thanks , I forgot that. It's been a while since I used C# . Although both versions work, i've edited my post.
Mervin
@Mervin: You need to pass a `char`, not a `string` (so: single quotes, not double quotes). I fixed it for you -- hopefully you don't mind.
Dan Tao
Your solution will produce output like "2." when it only got zeros.
jgauffin
@jgauffin I'll update my post, I looked over the dot by accident.
Mervin
gr8. Now you only have "0.0" left. Would procuce an empty string with your code.
jgauffin