string IFormattable.ToString(string format, IFormatProvider formatProvider) {
if (format == null) return this.ToString();
switch (format[0]) {
case 'd':
case 'D':
if (format.Length > 1) {
int precision = Convert.ToInt32(format.Substring(1), CultureInfo.InvariantCulture.NumberFormat);
string baseStr = ToString(10);
if (baseStr.Length < precision) {
string additional = new String('0', precision - baseStr.Length);
if (baseStr[0] != '-') {
return additional + baseStr;
} else {
return "-" + additional + baseStr.Substring(1);
}
}
return baseStr;
}
return ToString(10);
case 'x':
case 'X':
StringBuilder res = new StringBuilder(ToString(16));
if (format[0] == 'x') {
for (int i = 0; i < res.Length; i++) {
if (res[i] >= 'A' && res[i] <= 'F') {
res[i] = Char.ToLower(res[i], CultureInfo.InvariantCulture);
}
}
}
if (format.Length > 1) {
int precision = Convert.ToInt32(format.Substring(1), CultureInfo.InvariantCulture.NumberFormat);
if (res.Length < precision) {
string additional = new String('0', precision - res.Length);
if (res[0] != '-') {
res.Insert(0, additional);
} else {
res.Insert(1, additional);
}
}
}
return res.ToString();
default:
throw new NotImplementedException(MathResources.FormatNotImplemented);
}
}