开发者

Conditional FormatString or String.Format

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 FormatStrin开发者_Go百科g in markup (Silverlight). This is a case where I cant use a value converter (it's inside a style).

Is this possible?


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());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜