iformatprovider

Why DateTime.ParseExact(String, String, IFormatProvider) need the IFormatProvider?

帅比萌擦擦* 提交于 2019-12-18 10:37:38
问题 If we're using the ParseExact method for exact date-time's parsing using a specified format, why do we need to provide a IFormatProvider object? what is the point behind it? For example: DateTime.ParseExact(dateString, format, provider); Why do we need the provider here? 回答1: why do we need to provide a IFormatProvider object? what is the point behind it? It allows for culture-specific options. In particular: The format you use could be a standard date/time format, which means different

Using a custom formatter in a DataGridView

我只是一个虾纸丫 提交于 2019-12-17 19:52:44
问题 So, maybe this is a bad design; I don't know. But say I have a DataTable with a column that holds int values; these values are in fact meant to represent some enum type that I have in my project. What I'd like to do is have a DataGridView bound to this table and have the column display the name of the enum rather than the integer value "0" or "1" or whatever. One option I considered was to do the whole normalization thing: add a table in the DataSet with the enum names in it, keyed on the

How to use NumberFormatInfo to remove parenthesis for negative values

只谈情不闲聊 提交于 2019-12-17 16:26:20
问题 We're currently using the following for creating US dollar values in our web application: string.Format("{0:C0}", dollarValue ); In this example, if dollarValue is 145, then we'll see: $145. If it is -145, then we'll see ($145) rather than -$145. Note that for us, dollarValue is a double, but it could be any number-type. I think. Anyway, what I want is for it to be either $145 or -$145. Now, according to my research, it might be possible to do this using the NumberFormatInfo class. I can't

C# creating a custom NumberFormatInfo to display “Free” when a currency value is $0.00

∥☆過路亽.° 提交于 2019-12-12 12:03:08
问题 I need to display a currency in my ASP.NET MVC application but when the currency is 0 I would like it to display "Free" (localized of course!) instead of $0.00. So when I have something like this... Decimal priceFree = 0.00; Decimal priceNotFree = 100.00; priceFree.ToString("C"); priceNotFree.ToString("C"); The output is "$0.00" "$100.00" I would like it to be "Free" "$100.00" I imagine I can use the .ToString(string format, IFormatProvider formatProvider) method to accomplish this but I'm

Custom string formatter in C#

孤者浪人 提交于 2019-12-07 07:57:13
问题 String formatting in C#; Can I use it? Yes. Can I implement custom formatting? No. I need to write something where I can pass a set of custom formatting options to string.Format , which will have some effect on the particular item. at the moment I have something like this: string.Format("{0}", item); but I want to be able to do things with that item: string.Format("{0:lcase}", item); // lowercases the item string.Format("{0:ucase}", item); // uppercases the item string.Format("{0:nospace}",

Custom string formatter in C#

﹥>﹥吖頭↗ 提交于 2019-12-05 08:49:57
String formatting in C#; Can I use it? Yes. Can I implement custom formatting? No. I need to write something where I can pass a set of custom formatting options to string.Format , which will have some effect on the particular item. at the moment I have something like this: string.Format("{0}", item); but I want to be able to do things with that item: string.Format("{0:lcase}", item); // lowercases the item string.Format("{0:ucase}", item); // uppercases the item string.Format("{0:nospace}", item); // removes spaces I know I can do things like .ToUpper() , .ToLower() etc. but I need to do it

What does IFormatProvider do?

安稳与你 提交于 2019-12-03 01:50:55
问题 I was playing around with the Datetime.ParseExact method, and it wants an IFormatProvider... It works inputting null, but what exactly does it do? 回答1: In adition to Ian Boyd's answer: Also CultureInfo implements this interface and can be used in your case. So you could parse a French date string for example; you could use var ci = new CultureInfo("fr-FR"); DateTime dt = DateTime.ParseExact(yourDateInputString, yourFormatString, ci); 回答2: The IFormatProvider interface is normally implemented

What does IFormatProvider do?

谁说胖子不能爱 提交于 2019-12-02 15:41:49
I was playing around with the Datetime.ParseExact method, and it wants an IFormatProvider... It works inputting null, but what exactly does it do? In adition to Ian Boyd's answer: Also CultureInfo implements this interface and can be used in your case. So you could parse a French date string for example; you could use var ci = new CultureInfo("fr-FR"); DateTime dt = DateTime.ParseExact(yourDateInputString, yourFormatString, ci); The IFormatProvider interface is normally implemented for you by a CultureInfo class, e.g.: CultureInfo.CurrentCulture CultureInfo.CurrentUICulture CultureInfo

Odd decimal type behavior for ToString(IFormatProvider)

孤人 提交于 2019-12-02 10:28:03
问题 var numberFormat = new NumberFormatInfo(); numberFormat.NumberDecimalSeparator = "."; numberFormat.NumberDecimalDigits = 2; decimal a = 10.00M; decimal b = 10M; Console.WriteLine(a.ToString(numberFormat)); Console.WriteLine(b.ToString(numberFormat)); Console.WriteLine(a == b ? "True": "False"); In console: 10.00 10 True Why is it different? More important, how do I call ToString() to ensure same output no matter how a variable is initialized? 回答1: The NumberDecimalDigits property is used with

Odd decimal type behavior for ToString(IFormatProvider)

风格不统一 提交于 2019-12-02 06:01:01
var numberFormat = new NumberFormatInfo(); numberFormat.NumberDecimalSeparator = "."; numberFormat.NumberDecimalDigits = 2; decimal a = 10.00M; decimal b = 10M; Console.WriteLine(a.ToString(numberFormat)); Console.WriteLine(b.ToString(numberFormat)); Console.WriteLine(a == b ? "True": "False"); In console: 10.00 10 True Why is it different? More important, how do I call ToString() to ensure same output no matter how a variable is initialized? The NumberDecimalDigits property is used with the "F" and "N" standard format strings, not the ToString method called without a format string. You can