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

前端 未结 11 1165
予麋鹿
予麋鹿 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

    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
    

提交回复
热议问题