2014-02-27 46 views

回答

2

考虑写不论什么类型的号码是一个Extension Method(INT?多久?浮?)。这样,你可以这样做:

int playerSalary = 20000000; 
Print(playerSalary.ToLargeCurrencyFormat()); 
// Prints $20M 

扩展方法:

namespace ExtensionMethods 
{ 
    public static class IntExtensions 
    { 
     public static string ToLargeCurrencyFormat(this int amount) 
     { 
      // Do your format conversion here 
      return formattedString; 
     } 
    } 
} 
0

没有参数ToString方法,可以自动格式化您的货币值的方式。

但是,您可以轻松地编写自己的方法来以这种方式格式化字符串。

编写if/else逻辑来获得显示字符串是微不足道的,您可以按照您在问题中引用的帖子中提供的示例代码。

如果你想变得有趣,你可能需要考虑实现一个自定义格式提供者。

MSDN documentation on IFormatProviderpost on SO.