如何在@Html.TextBoxFor()方法中禁止科学格式化数字?

huangapple go评论70阅读模式
英文:

How to do not allow scientific formatting for numbers in @Html.TextBoxFor() method?

问题

我遇到了同样的问题,当使用该方法时,它会将小的双精度数转换为科学计数法(0.000028 -> 2.8e-005)。

我对此进行了一些研究,没有找到任何可以让我继续使用@Html.TextBoxFor()并按照我想要的方式显示数据的方法。基本上,我看到的解决方案要么是将元素更改为Html.EditorFor()或Html.DisplayFor(),并在它们上创建一些模板。

由于项目可能在一个表单上有数百个输入框,我找到的解决方案会过于复杂。有没有办法保持这种方法并按照我想要的方式呈现数据?

英文:

as there I have the same problem, while using the method it converts small double to scientific notation (0.000028 -> 2.8e-005).

I did a little research on that and didn't find anything that would let me use @Html.TextBoxFor() still and show data as I want. Basically, the solutions I see are either change the element to Html.EditorFor() or Html.DisplayFor() and create some template on them.

As the project may have hundreds of input boxes on one form the solution I've found will be an overkill. Is there any way to keep the method and render data as I want?

答案1

得分: 1

我认为以下可能是解决方案:

@Html.TextBoxFor(m => m.Data, "{0:#,0.######}")

其中 Datadouble 类型。

此外,可以定义 number 类型以改进客户端验证。例如:

@Html.TextBoxFor(m => m.Data, "{0:#,0.######}", new { type = "number", @min=-99999, @max=99999, @step="any" })
英文:

I suppose the following might be solution:

@Html.TextBoxFor(m => m.Data, "{0:#,0.######}")

Where Data is double.

<hr>
Moreover, the number type might be defined to improve validation on the client side. For example:

@Html.TextBoxFor(m =&gt; m.Data, &quot;{0:#,0.######}&quot;, new { type = &quot;number&quot;, @min=-99999, @max=99999, @step=&quot;any&quot; })

huangapple
  • 本文由 发表于 2023年8月8日 22:01:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860304.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定