条件FormatString或String.Format

发布于 2024-10-09 14:10:47 字数 322 浏览 1 评论 0原文

我看到了这个帖子: 如何在 .Net 中条件格式化字符串?

第一个问题的一部分指向使用条件格式的能力。这是怎么做到的?

就我而言,我想将 100,000 计算为 10 万美元,将 1,000,000 计算为 100 万美元。我希望能够仅使用标记中的 FormatString 来完成此操作(Silverlight)。在这种情况下,我无法使用值转换器(它位于样式内部)。

这可能吗?

I saw this post:
How to Conditionally Format a String in .Net?

The first part of the question points to the ability to use conditional formats. How is this done?

In my case, I want to do for instance 100,000 as $100k, and 1,000,000 as $1m. I want to be able to do it with just the FormatString in markup (Silverlight). This is a case where I cant use a value converter (it's inside a style).

Is this possible?

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

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

发布评论

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

评论(1

浅语花开 2024-10-16 14:10:47

您可以实现自己的 IFormatProvider 并定义 ie。自定义并在调用 String.Format()ToString() 时传递该值。

可以在此处找到此示例 http://msdn.microsoft.com/ en-us/library/system.iformatprovider.aspx 或此处 http:// www.codeproject.com/KB/cs/custstrformat.aspx

public class StringFormatInfo : IFormatProvider, ICustomFormatter
{
   ...
}

return number.ToString("{0:custom}", new StringFormatInfo());

You can implement your own IFormatProvider and define ie. custom and pass that when calling String.Format() or ToString().

Example of this can be found here http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx or here http://www.codeproject.com/KB/cs/custstrformat.aspx.

public class StringFormatInfo : IFormatProvider, ICustomFormatter
{
   ...
}

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