format a number with commas and decimals in C# (asp.net MVC3)

前端 未结 11 1109
予麋鹿
予麋鹿 2020-11-29 03:40

I Need to display a number with commas and decimal point.

Eg: Case 1 : Decimal number is 432324 (This does not have commas or decimal Points) Need to display it

相关标签:
11条回答
  • 2020-11-29 04:37

    I had the same problem. I wanted to format numbers like the "General" format in spreadsheets, meaning show decimals if they're significant, but chop them off if not. In other words:

    1234.56 => 1,234.56

    1234 => 1,234

    It needs to support a maximum number of places after the decimal, but don't put trailing zeros or dots if not required, and of course, it needs to be culture friendly. I never really figured out a clean way to do it using String.Format alone, but a combination of String.Format and Regex.Replace with some culture help from NumberFormatInfo.CurrentInfo did the job (LinqPad C# Program).

    string FormatNumber<T>(T number, int maxDecimals = 4) {
        return Regex.Replace(String.Format("{0:n" + maxDecimals + "}", number),
                             @"[" + System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator + "]?0+$", "");
    }   
    
    void Main(){
        foreach (var test in new[] { 123, 1234, 1234.56, 123456.789, 1234.56789123 } )
            Console.WriteLine(test + " = " + FormatNumber(test));
    }
    

    Produces:

    123 = 123
    1234 = 1,234
    1234.56 = 1,234.56
    123456.789 = 123,456.789
    1234.56789123 = 1,234.5679
    
    0 讨论(0)
  • 2020-11-29 04:37

    If you are using string variables you can format the string directly using a : then specify the format (e.g. N0, P2, etc).

    decimal Number = 2000.55512016465m;
    $"{Number:N}" #Outputs 2,000.55512016465
    

    You can also specify the number of decimal places to show by adding a number to the end like

    $"{Number:N1}" #Outputs 2,000.5
    $"{Number:N2}" #Outputs 2,000.55
    $"{Number:N3}" #Outputs 2,000.555
    $"{Number:N4}" #Outputs 2,000.5551
    
    0 讨论(0)
  • 2020-11-29 04:38

    Maybe you simply want the standard format string "N", as in

    number.ToString("N")
    

    It will use thousand separators, and a fixed number of fractional decimals. The symbol for thousands separators and the symbol for the decimal point depend on the format provider (typically CultureInfo) you use, as does the number of decimals (which will normally by 2, as you require).

    If the format provider specifies a different number of decimals, and if you don't want to change the format provider, you can give the number of decimals after the N, as in .ToString("N2").

    Edit: The sizes of the groups between the commas are governed by the

    CultureInfo.CurrentCulture.NumberFormat.NumberGroupSizes
    

    array, given that you don't specify a special format provider.

    0 讨论(0)
  • 2020-11-29 04:39

    Try with

    ToString("#,##0.###")
    

    Produces:

    1234.55678 => 1,234.556

    1234 => 1,234

    0 讨论(0)
  • 2020-11-29 04:40

    Try with

    ToString("#,##0.00")
    

    From MSDN

    *The "0" custom format specifier serves as a zero-placeholder symbol. If the value that is being formatted has a digit in the position where the zero appears in the format string, that digit is copied to the result string; otherwise, a zero appears in the result string. The position of the leftmost zero before the decimal point and the rightmost zero after the decimal point determines the range of digits that are always present in the result string.

    The "00" specifier causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used. For example, formatting 34.5 with "00" would result in the value 35.*

    0 讨论(0)
提交回复
热议问题