Support ToString (string format) with custom number type

Having created my own type of number (actually DoubleDouble ), I want to implement the IFormattable interface. So I need to somehow parse the format string.

 public string ToString(string format, IFormatProvider formatProvider) { // formatting string according to format and using formatprovider? return formattedString; } 

The class user should be able to use it as a replacement for double (or any other number format).

 String.Format("{0:0.##}", (DoubleDouble)123.4567); 

My question is: does anyone know a good tutorial about this, or can give me some tips? How to maintain localization in this process?

How to parse a format string? Are there any methods to help with this task, or should I do all of this with a regexp hand, etc.

I really looked for help, but could not find it, if you find something in another language (C, C ++) that can help, please tell me about it.

+8
string tostring c # formatting
source share
4 answers

MSDN has a good example of the Temperature class, which implements the IFormattable interface with its own custom format.

I think you already know this; anyway, today I found out that if your DoubleDouble class implemented the IFormattable interface, then:

 String.Format("{0:0.##}", (DoubleDouble)123.4567); 

... will call the implementation of the DoubleDouble class ToString(...) with the specific format "0.##" as the first parameter, which, I suspect, is close to what you want. You should still parse this part of the format.

I would jeopardize that most of the parsing of the format is embedded deep in the highly optimized binaries of the .Net library, so you cannot use any custom virtual methods for analysis.

+1
source share

How to maintain localization in this process?

Something like the following:

 public string ToString(string format, IFormatProvider formatProvider) { CultureInfo culture = formatProvider as CultureInfo; if (culture != null) { // Now you can do things like // culture.NumberFormat.NumberDecimalSeparator, etc. } } 

How to parse a format string? Are there any methods to help with this, or should I do all of this with a "regex" with a regular expression, etc.

There is no public API in the .NET Framework to do this for you. If you look at source for a .NET implementation of primitive numeric IFormattable types, all of them will eventually call external methods, so they give no understanding. You will probably need to use something like RegEx to parse the format string. You can then divide by the highest power of 10 to find out what the first digit of the output will be, subtract this, and then repeat for the lower power of 10 until you have generated all the digits.

0
source share

Perhaps this may help you:

 var my = DoFormat(123.0) public static string DoFormat( double myNumber ) { var s = string.Format("{0:0.00}", myNumber); if ( s.EndsWith("00") ) { return ((int)myNumber).ToString(); } else { return s; } } 
0
source share

A few years ago, I wrote a JavaScript formatting function that mimicked most of the ways that .NET strings are formatted. This can help you write a C # version. At the very least, this can give you a starting point.

https://github.com/flamewave/jquery-ui-numeric/blob/master/jquery-ui-numeric.js

Scroll all the way down to the $ .formatNumber function.

Note. I no longer support this feature in favor of using the Globalize library, which can also give you some idea of ​​the string parsing format.

0
source share

All Articles