Trace and trace

Source: Internet
Author: User

Trace and trace

Click to download sample code

String. Format: the signature of the load method is as follows:

1 public static string Format(2     IFormatProvider provider,3     string format,4     params Object[] args5 )

You can use the custom IFormatProvider interface to control specific behaviors during String. Format execution.

 

The procedure is as follows:

1. Implement the IFormatProvider interface (object GetFormat method) for the custom class)

1 public object GetFormat(Type formatType)2 {3     if (formatType == typeof(ICustomFormatter))4         return this;5     else6         return null;7 }

 

2. Implementation of ICustomFormatter (string Format method)

From MSDN. Function is used to control the string output of Int64 type.

 1 public string Format(string fmt, object arg, IFormatProvider formatProvider) 2 { 3     // Provide default formatting if arg is not an Int64.  4     if (arg.GetType() != typeof(Int64)) 5         try 6         { 7             return HandleOtherFormats(fmt, arg); 8         } 9         catch (FormatException e)10         {11             throw new FormatException(String.Format("The format of '{0}' is invalid.", fmt), e);12         }13 14     // Provide default formatting for unsupported format strings. 15     string ufmt = fmt.ToUpper(CultureInfo.InvariantCulture);16     if (!(ufmt == "H" || ufmt == "I"))17         try18         {19             return HandleOtherFormats(fmt, arg);20         }21         catch (FormatException e)22         {23             throw new FormatException(String.Format("The format of '{0}' is invalid.", fmt), e);24         }25 26     // Convert argument to a string. 27     string result = arg.ToString();28 29     // If account number is less than 12 characters, pad with leading zeroes. 30     if (result.Length < ACCT_LENGTH)31         result = result.PadLeft(ACCT_LENGTH, '0');32     // If account number is more than 12 characters, truncate to 12 characters. 33     if (result.Length > ACCT_LENGTH)34         result = result.Substring(0, ACCT_LENGTH);35 36     if (ufmt == "I")                    // Integer-only format.  37         return result;38     // Add hyphens for H format specifier. 39     else                                          // Hyphenated format. 40         return result.Substring(0, 5) + "-" + result.Substring(5, 3) + "-" + result.Substring(8);41 }

 

Use the implemented Format class

 1 long acctNumber; 2 double balance; 3 DaysOfWeek wday; 4 string output; 5  6 acctNumber = 104254567890; 7 balance = 16.34; 8 wday = DaysOfWeek.Monday; 9 10 output = String.Format((new AcctNumberFormat(),11                        "On {2}, the balance of account {0:H} was {1:C2}.",12                        acctNumber, balance, wday);13 Console.WriteLine(output);

 

The execution result is as follows:

 

References

Culture invariant Decimal. TryParse () http://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse

What does IFormatProvider do? Http://stackoverflow.com/questions/506676/what-does-iformatprovider-do

IFormatProvider Interface https://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.