formatprovider

Best practices for serializing objects to a custom string format for use in an output file

▼魔方 西西 提交于 2019-11-27 00:09:57
I was just about to implement an override of ToString() on a particular business class in order to produce an Excel-friendly format to write to an output file, which will be picked up later and processed. Here's what the data is supposed to look like: 5555555 "LASTN SR, FIRSTN" 5555555555 13956 STREET RD TOWNSVILLE MI 48890 25.88 01-003-06-0934 It's no big deal for me to just make a format string and override ToString() , but that will change the behavior of ToString() for any objects I decide to serialize this way, making the implementation of ToString() all ragged across the library. Now, I

Format decimal for percentage values?

本秂侑毒 提交于 2019-11-26 09:19:30
问题 What I want is something like this: String.Format(\"Value: {0:%%}.\", 0.8526) Where %% is that format provider or whatever I am looking for. Should result: Value: %85.26. . I basically need it for wpf binding, but first let\'s solve the general formatting issue: <TextBlock Text=\"{Binding Percent, StringFormat=%%}\" /> 回答1: Use the P format string. This will vary by culture: String.Format("Value: {0:P2}.", 0.8526) // formats as 85.26 % (varies by culture) 回答2: If you have a good reason to set

Best practices for serializing objects to a custom string format for use in an output file

无人久伴 提交于 2019-11-26 09:17:52
问题 I was just about to implement an override of ToString() on a particular business class in order to produce an Excel-friendly format to write to an output file, which will be picked up later and processed. Here\'s what the data is supposed to look like: 5555555 \"LASTN SR, FIRSTN\" 5555555555 13956 STREET RD TOWNSVILLE MI 48890 25.88 01-003-06-0934 It\'s no big deal for me to just make a format string and override ToString() , but that will change the behavior of ToString() for any objects I