Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit to 2 decimals in TextBoxFor

The code below works fine but, in the textbox the decimal value has this format "0,0000" (, is the decimal separator). I'd like have only 2 decimal. How can I do this ?

Thanks,

//Database model used with NHibernate public class Bank {     public virtual int Id { get; set; }     public virtual string FirstName { get; set; }     public virtual string LastName{ get; set; }     public virtual decimal Amount { get; set; } }  //MVC Model public class MyModel {     public Bank Bank { get; set; }   }  //View @Html.TextBoxFor(m => m.Bank.Amount, new { id = "tbAmount"})  

Update 1

In the debugger, I don't see any decimal, wehn I do step by step inside (o @HTML.Textbofor) the view, the value does not have any decimal but when the page is displayed there are 4 decimals

//Database model used with NHibernate public class Bank {     public virtual int Id { get; set; }     public virtual string FirstName { get; set; }     public virtual string LastName{ get; set; }     public virtual decimal Amount { get; set; } }  //Class for view public class ViewBank {     [DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]     public decimal Amount { get; set; } }  //MVC Model public class MyModel {     public Bank Bank { get; set; }           var ViewBank = new ViewBank() { Amount = Bank.Amount}; }  //View @Html.TextBoxFor(m => m.Amount, new { id = "tbAmount"})  
like image 244
Kris-I Avatar asked May 25 '11 06:05

Kris-I


People also ask

How do you limit two decimal places?

Rounding a decimal number to two decimal places is the same as rounding it to the hundredths place, which is the second place to the right of the decimal point. For example, 2.83620364 can be round to two decimal places as 2.84, and 0.7035 can be round to two decimal places as 0.70.

How do you restrict a float upto 2 decimal places?

We will use %. 2f to limit a given floating-point number to two decimal places.

How do I limit decimal places in Perl?

The f format lets you specify a particular number of decimal places to round its argument to. Perl looks at the following digit, rounds up if it is 5 or greater, and rounds down otherwise. Three functions that may be useful if you want to round a floating-point value to an integral value are int , ceil , and floor .


2 Answers

I would use editor templates and I would not use my NHibernate domain models in my views. I would define view models which are specifically tailored to the requirements of the given view (in this case limiting the amount to 2 decimals):

[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)] public decimal Amount { get; set; } 

and then:

@Html.EditorFor(m => m.Bank.Amount)  
like image 131
Darin Dimitrov Avatar answered Sep 21 '22 12:09

Darin Dimitrov


This works for me

@Html.TextBox("Amount", String.Format("{0:0.00}", Model.Bank.Amount), new { id = "tbAmount"}) 

EDIT:

This is for TextBoxFor (does not work on MVC3)

@{var formated = String.Format("{0:0.00}", Model.Bank.Amount);} @Html.TextBoxFor(m => m.Bank.Amount, formated, new { id = "tbAmount"}) 
like image 37
Alexandros B Avatar answered Sep 22 '22 12:09

Alexandros B