2

In my application certain fields contain decimal numbers. And it is possible to enter only 2 decimal points. After doing some calculations, the result is displayed, and the answer should also contain 2 decimal points. For that I used the code below:-

obj.Revenue = 0.00m;

obj.Revenue = Math.Round(Convert.ToDecimal(obj.Revenue), 2);

[EDIT]

Inside view:-

@Html.TextBoxFor(model => model.Revenue, new { @id = "txtRevenue", @readonly = "readonly", @class = "decimalField" })

So that 300.00 should be displayed as answer. But I got 300. How can I solve this?

Mathias
  • 15,191
  • 9
  • 60
  • 92
Mizbella
  • 936
  • 2
  • 15
  • 30

5 Answers5

0

Use toFixed(n);

var d = 300.00;
alert(d.toFixed(2));
Rick
  • 153
  • 2
  • 10
0

Use toFixed() function

For example:

(12.5).toFixed(2); // 12.50
David Rego
  • 163
  • 2
  • 13
  • Got error like this:-'decimal' does not contain a definition for 'toFixed' and no extension method 'toFixed' accepting a first argument of type 'decimal' could be found – Mizbella May 17 '13 at 14:59
  • @Mizbella try this: string test=String.Format("{0:0.00}", 300); var convert = Convert.ToDecimal(test); – David Rego May 17 '13 at 15:20
  • @ David Rego:-while converting to decimal .00 is lost – Mizbella May 17 '13 at 15:31
  • i try to print with convert value and shows me 300.00. string test=String.Format("{0:0.00}", 300); var convert = Convert.ToDecimal(test);Console.WriteLine(convert); – David Rego May 17 '13 at 15:37
0

Try this:

@Html.TextBoxFor(model => model.Revenue, new {value = Model.Revenue.ToString("#.##"), @id = "txtRevenue", @readonly = "readonly", @class = "decimalField" })
Kaizen Programmer
  • 3,798
  • 1
  • 14
  • 30
0

You need to implement get accessor for the model.Revenue property this way:

get{ return _revenue.ToString("#.##"); }
Oleg Ignatov
  • 877
  • 2
  • 8
  • 22
0

I know its too late to answer this question but it may help other.

You can simply write this line in your model where you have declared the property. [DisplayFormat(DataFormatString = "{0:0.###}")]

here is the practical example which i used in my code.

[DisplayFormat(DataFormatString = "{0:0.##}")]
    public Decimal CreditLimit { get; set; }

this will set your value up to two decimal.

e.g. 3.3333333 => 3.33

Syed Uzair Uddin
  • 3,296
  • 7
  • 31
  • 47