.netcore asp-for decimal loses decimal places

孤街浪徒 提交于 2020-06-16 05:31:20

问题


I am running into an issue where I am losing decimal places when using the asp-for tag helper in my view. It seems to always round up to 2 decimals, while I want to have it round up to 3 decimals (in this case).

I can see in my debugger that the value in the Model has 3 decimal places as e

But then in my view, it's being displayed as 1.28 instead of 1.275:

This is my form's view, as you can see there's nothing special really going on (the asp-has-error tag helper is a custom tag helper for this project):

<div class="form-group row">
        <label asp-for="RatePercentage" class="col-lg-4 col-form-label"></label>
        <div class="col-lg-8">
                <input asp-for="RatePercentage" asp-has-error="@Html.HasErrorFor(m => m.RatePercentage)" class="form-control"/>
        </div>
</div>

Any ideas how I can get 3 decimal places to show here?


回答1:


That's just the default. You can use the DisplayFormat attribute to make it whatever you like:

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

Where [format] is the format string you want. See Standard Numeric Format Strings and Custom Numeric Format Strings.



来源:https://stackoverflow.com/questions/50985740/netcore-asp-for-decimal-loses-decimal-places

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!