Format a Number with Commas and Decimals in C# (ASP.NET MVC3)

Format a number with commas and decimals in C#

int number = 1234567890;
number.ToString("#,##0.00");

You will get the result 1,234,567,890.00.

How to format decimal in MVC3 with more than 2 decimals

I managed to make it as follows:

  1. For display I used

    @string.Format("{0:f4}", Model.KPINumber)
  2. for edit I used

    @Html.TextBox("KPINumber", string.Format("{0:f4}", Model.KPINumber))

Using Razor view engine - how do I format a decimal value to have commas and two decimal places?

Can you use {0:c} instead? This is just standard string formatting in .NET and the "c" is for currency. There are lots of standard numeric string formats. And, of course, custom formatting, too.

Format a number with commas, but keep decimals

This appears to do exactly what you want:

public void Code(params string[] args)
{
Print(1000);
Print(100000);
Print(123.456);
Print(100000.21 );
Print(100200.123456);
}

void Print(double n)
{
Console.WriteLine("{0:###,###.#######}", n);
}

1,000
100,000
123.456
100,000.21
100,200.123456

display number with commas and decimal points

Assuming you want the usual 3 numbers then a comma, I think this will do what you need:

Label9.Text = sisRatio.ToString("#,##0.##");

One slight issue with this is that it will only one decimal place if the second one would be 0

Decimal values with thousand separator in Asp.Net MVC

It seems there are always workarounds of some form or another to be found in order to make the default model binder happy! I wonder if you could create a "pseudo" property that is used only by the model binder? (Note, this is by no means elegant. Myself, I seem to resort to similar tricks like this more and more often simply because they work and they get the job "done"...) Note also, if you were using a separate "ViewModel" (which I recommend for this), you could put this code in there, and leave your domain model nice and clean.

public class Employee
{
private decimal _Salary;
public string MvcSalary // yes, a string. Bind your form values to this!
{
get { return _Salary.ToString(); }
set
{
// (Using some pseudo-code here in this pseudo-property!)
if (AppearsToBeValidDecimal(value)) {
_Salary = StripCommas(value);
}
}
}
public decimal Salary
{
get { return _Salary; }
set { _Salary = value; }
}
}

P.S., after I typed this up, I look back at it now and am even hesitating to post this, it is so ugly! But if you think it might be helpful I'll let you decide...

Best of luck!

-Mike

Accept comma and dot as decimal separator

Cleanest way is to implement your own model binder

public class DecimalModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

return valueProviderResult == null ? base.BindModel(controllerContext, bindingContext) : Convert.ToDecimal(valueProviderResult.AttemptedValue);
// of course replace with your custom conversion logic
}
}

And register it inside Application_Start():

ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder());

Credits : Default ASP.NET MVC 3 model binder doesn't bind decimal properties

MVC3 Number format

Yeah, CultureInfo. Store things internaly using a predefined CultureInfo (whatever is local to your region). Associate the radion button values to cultures that are using dot (english) or comma(french) and then serve the values using the chosen culture info.

The point is you keep the values internally using ONE format then use the desired culture info to display them.



Related Topics



Leave a reply



Submit