.netcore asp-for十进制失去小数位 [英] .netcore asp-for decimal loses decimal places

查看:223
本文介绍了.netcore asp-for十进制失去小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,在我的视图中使用asp-for标签帮助程序时,我失去了小数位.似乎总是四舍五入到小数点后两位,而我想让它四舍五入到小数点后三位(在这种情况下).

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).

我在调试器中看到模型中的值具有3个小数位,例如e

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

但是在我看来,它显示为1.28而不是1.275:

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

这是我表单的视图,如您所见,实际上并没有什么特别的事情(asp-has-error标签帮助程序是该项目的自定义标签帮助程序):

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>

有什么想法可以让我显示3个小数位吗?

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

推荐答案

这只是默认设置.您可以使用DisplayFormat属性将其设置为任意值:

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; }

其中[format]是所需的格式字符串.请参见标准数字格式字符串自定义数字格式字符串.

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

这篇关于.netcore asp-for十进制失去小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆