将数字格式设置为右对齐,同时保持货币符号左对齐

发布于 2024-08-02 06:14:09 字数 358 浏览 1 评论 0原文

是否有内置的 FormatString 或使用自定义 FormatString 的方法:
$150.00
$1,170.00
$12,170.00
$90.00
$38.00
$750.00

并将它们格式化为:

$    150.00
$  1,170.00
$ 12,170.00
$     90.00
$     38.00
$    750.00

不知道最大值?这是在 gridview 绑定列中 我目前位于 rowdatabound 的代码隐藏中,循环遍历现有行以获取最大的行,并在必要时更改当前行,或者如果当前行是最大的则更改现有行,所以我有一种解决方法,但希望有一个干净的解决方案。

Is there a built in FormatString or a way to use a custom FormatString to take:
$150.00
$1,170.00
$12,170.00
$90.00
$38.00
$750.00

And format them like:

$    150.00
$  1,170.00
$ 12,170.00
$     90.00
$     38.00
$    750.00

without knowing the largest value? This is in a gridview boundcolumn
I am currently in the codebehind on rowdatabound looping through the existing rows to get the largest and changing the current one if necessary or changing the existing ones if the current one is the largest, so I have a workaround but was hoping for a clean solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟织青萝梦 2024-08-09 06:14:10

据我所知,如果不知道最大长度,就无法用空格填充字符串,并且要获得最大长度,您需要知道最大值。

您是否考虑过在列上使用对齐属性?您将得到以下内容,这并不完全是您正在寻找的内容。

   $150.00
 $1,170.00
$12,170.00
    $90.00
    $38.00
   $750.00

To the best of my knowledge, there is no way to pad a string with spaces without knowing the maximum length and to get the maximum length you would need to know the maximum value.

Have you considered the use of an alignment property on the column? You would get the following, which is not quite exactly what you are looking for.

   $150.00
 $1,170.00
$12,170.00
    $90.00
    $38.00
   $750.00
亢潮 2024-08-09 06:14:09

如果您有数字,并且货币相同,为什么不在网格中使用模板列,让一个跨度随货币向左浮动,一个跨度随金额向右浮动。可以省去很多麻烦...

If you have the numbers, and the currency will be the same, why not just use a template column in your grid, have a span floated left with the currency and a span floated right with the amount. Could save a lot of hassle...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文