1

I have this custom extension that should format a decimal number with a custom amount of digits after comma.

public static decimal FormatDecimal(this decimal value, int decimalSeparator = 2)
{
    decimal returnValue = Math.Round(value, decimalSeparator, MidpointRounding.AwayFromZero);
    return returnValue;
}

The problem is that doesn't work as expected.

If I do like this:

decimal number = 12345;

and then:

decimal formatedNumber = number.FormatDecimal(2);

the result should be:

12345.00

instead the result is:

12345

What I am doing wrong?

3
  • 2
    12345 and 12345.00 are equal as a numeric value. How do you display your decimal value exactly? Looks like a textual representation issue. Commented Apr 19, 2015 at 12:14
  • @SonerGönül: I just have it .ToString(). However, if the return value is 12345.44 then .ToString() shows it correctly. Except if it's .00 Commented Apr 19, 2015 at 12:19
  • number.ToString("#.00") Commented Apr 19, 2015 at 12:22

4 Answers 4

1

Here's the extension function working

    public static string FormatDecimal(this decimal value, int decimalSeparator = 2)
    {
        return  value.ToString(string.Format("0.{0}", new string('0', decimalSeparator)));
    }
Sign up to request clarification or add additional context in comments.

2 Comments

This works perfect. However I have one question: If the value is decimal value is 0 this returns .00. How can I make it so that returns 0.00 ?
substitute "#.{0}" with "0.{0}" (I'll do it in the answer too :-)
1

I think the right way is to using The "0" custom format specifier;

Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.

For example;

decimal d = 12345;
Console.WriteLine(d.ToString("#.00")); // 12345.00

Comments

0

You're probably looking to format the string representation of your decimal instead. Try this:

decimal myNumber = 12345.67m;
string formattedNumber = myNumber.ToString("N3");
Console.WriteLine(formattedNumber); // Prints "12345.670"

See here for more information: https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx

From MSDN:

Standard numeric format strings are used to format common numeric types. A standard numeric format string takes the form Axx, where:

A is a single alphabetic character called the format specifier. Any numeric format string that contains more than one alphabetic character, including white space, is interpreted as a custom numeric format string. For more information, see Custom Numeric Format Strings.

xx is an optional integer called the precision specifier. The precision specifier ranges from 0 to 99 and affects the number of digits in the result. Note that the precision specifier controls the number of digits in the string representation of a number. It does not round the number itself. To perform a rounding operation, use the Math.Ceiling, Math.Floor, or Math.Round method.

When precision specifier controls the number of fractional digits in the result string, the result strings reflect numbers that are rounded away from zero (that is, using MidpointRounding.AwayFromZero).

2 Comments

I don't think using N format specifier is the right way in this case because it formats your value with group separator as well. That's why it will be like 12,345 instead of 12345.
Good point. I personally consider that to be a nice extra feature, but I guess not everyone will feel the same way.
0

You should specify the formatting when you display the string.

What you need is to do the following, when you convert to string:

String.Format("{0:0.00}", formatedNumber);

Refer to This article for more details:

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.