5

In my database I stored fields with the data type decimal. I am using exactly the same (decimal) data type in my ASP.NET application.

This is inside my view in order to display the value.

@Html.TextBoxFor(model => model.Stock, new { id = "Stock", @class = "k-textbox" })

This pretty straight forward. The only issue I am facing is that by default in my view the data is displayed with 4 decimal places.

I give you a few examples on how it looks and how it should look:

  1. 1,0000 => 1
  2. 1,2000 => 1,2
  3. 1,4300 => 1,43
  4. 1,8920 => 1,892
  5. 1,5426 => 1,5426

As you can see I want to cut off all the 0 as decimal places (only when displayed in the view).

Remember: I am using commas instead of decimal points.

Edit:

My model

public class Article
{
    public decimal? Stock{ get; set; }
}
Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102
  • Not related but `new { id = "Stock" }` is pointless - the method already generates it. You can use one of the overloads which accepts a `DataFormatString` –  Jul 12 '17 at 13:47

3 Answers3

8

The G29 argument of the string.Format method does exactly what you want.

You can use the the following attribute on your models Stock value.

[DisplayFormat(DataFormatString = "{0:G29}", ApplyFormatInEditMode = true)]

Or you can use the overload which @Chris mentioned.

@Html.TextBoxFor(model => model.Stock, "{0:G29}", new { id = "Stock", @class = "k-textbox" })
NtFreX
  • 10,379
  • 2
  • 43
  • 63
  • But the data type is decimal in my model. Isn't the displayformat for strings? I made an edit of my post. – Tom el Safadi Jul 12 '17 at 20:11
  • And the overload method "G29" has to be in front of the custom css. – Tom el Safadi Jul 12 '17 at 20:13
  • These two methods seem like a very good approach. I would like to use it in my code. Thanks!! I just don't know how to use the displayformat as it doesn't change someting. I guess it is because it is not a string? – Tom el Safadi Jul 12 '17 at 20:15
  • the overload method doesn't change something. It looks still the same as before. – Tom el Safadi Jul 12 '17 at 20:22
  • See this post, thats the error I get: https://stackoverflow.com/questions/21754071/templates-can-be-used-only-with-field-access-property-access-single-dimension – Tom el Safadi Jul 12 '17 at 20:45
  • I think there is only the solution as S. Akbari said although it doesn't look very clean. – Tom el Safadi Jul 12 '17 at 20:47
  • @Anokrize hm strange that the second doesn't work. Did you try the latest one? I would consider using the view model as the accepted answer of the question you posted here sugessted. – NtFreX Jul 12 '17 at 20:49
  • Yeah I tried it but it doesn't work.... Really strange. It doesn't give me an error but it doesn't change something. – Tom el Safadi Jul 12 '17 at 21:06
3

You can use {0:G29} like this:

@{
    string temp = string.Format("{0:G29}", decimal.Parse(Model.Stock.ToString()));
    @Html.TextBoxFor(model => temp)
}

Or with string interpolation:

@{
    string temp = $"{decimal.Parse(Model.Stock.ToString()):G29}";
    @Html.TextBoxFor(model => temp)
}

EDIT: The reason that you can't get the value in your Controller after you save it is that the model binder can't find a property with name temp. You can use a TextBox instead of TextBoxFor to solve this issue like this:

string temp = $"{decimal.Parse(Model.Stock.ToString()):G29}";
@Html.TextBox("Stock" , temp)

Or if you still want to use TextBoxFor you can just rename the temp variable to Stock:

string Stock = string.Format("{0:G29}", decimal.Parse(Model.Stock.ToString()));
@Html.TextBoxFor(model => Stock)
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • Thanks that looks pretty good, but it gives me the error conversion from decima? in strang not possible. – Tom el Safadi Jul 12 '17 at 13:51
  • This works but I have to put a .ToString() at the end of it. Like: decimal.Parse(Model.Stock.ToString()) – Tom el Safadi Jul 12 '17 at 14:02
  • I can use it like this: string temp = $"{Model.Bestand:G29}"; – Tom el Safadi Jul 12 '17 at 14:06
  • In my model it already is decimal not a string so I don't have to parse it, right? – Tom el Safadi Jul 12 '17 at 14:06
  • There is a problem though. If I save it, it doesn't take the value. I am pretty sure that is because it isn't hooked up anymore. Do you know how to solve it? – Tom el Safadi Jul 12 '17 at 21:04
  • @Anokrize OK. The reason is that the model binder can't find a property with name `temp`. I've provided the solution for that. Check my updated answer. – Salah Akbari Jul 13 '17 at 05:01
  • @Anokrize And if you still want to use `TextBoxFor` you can just rename the `temp` variable to `Stock`. – Salah Akbari Jul 13 '17 at 05:20
  • Great, did it!! – Tom el Safadi Jul 13 '17 at 18:13
  • Why do you convert to string, then to decimal and finally to string with 2 places again using string.Format("{0:G29}", decimal.Parse(Model.Stock.ToString())); ? Why not to simple use directly string.Format("{0:G29}", Model.Stock); or Model.Stock.ToString("G29") ? Is not the same? I think so – Willy Feb 26 '21 at 13:10
1

There is an overload of TextBoxFor that takes a format parameter that it uses to format the text.

This will allow you to format your number in any way you want.

Chris
  • 27,210
  • 6
  • 71
  • 92