IFormatProvider从double到string - 数字数字的科学转换 [英] IFormatProvider scientific conversion from double to string - number of digits
问题描述
我想转换:
双重值:0.0772486324655191
字符串值:0.0772486324655191
如果长度大于小数点后的16位数,我想要这样:
double value:0.00063500244832493823
字符串值:6.3500244832493823e-004
我已尝试将其转换为IFormatProvider模式:
0.0000000000000000e000
但是,第一种情况的结果是
7.7248632465519100e-002
我如何获取我的双向量中的数字位数?或者更好:如何正确使用格式提供程序?
字符串说明符;文化文化;
specifier =0.0000000000000000e000;
culture = CultureInfo.CreateSpecificCulture(en-US);
Console.WriteLine(DoubleMirrored [0] .ToString(specifier,CultureInfo.InvariantCulture));
为此,您绝对需要创建一个自定义格式化。
要创建自定义格式化程序,您应该了解以下内容:
string.Format
有以下重载: string.Format(IFormatProvider,string,object [])
,所以你必须创建一个 IFormatProvider
这将提供一个 ICustomFormatter
,它将处理您的自定义格式。
这里有一些代码完全符合您的描述:
public class DoubleFormatter:IFormatProvider,ICustomFormatter
{
//实现IFormatProvider:
public object GetFormat(Type t){
if( t == typeof(ICustomFormatter)){
return this;
}
返回null;
}
//实现ICustomFormatter:
public string Format(string format,object arg,IFormatProvider provider){
//搜索自定义的EE格式说明符:
if(format == null ||!format.StartsWith(EE))return null;
format = format.Substring(2); //修剪EE
//确定截止前有多少个数字:
int数字;
if(!int.TryParse(format,out digits)){
throw new FormatException(Format must contain digits);
}
//获取值:(注意,这将适用于任何数字类型)
var value = Convert.ToDouble(arg);
//转换为字符串而不使用指数格式:
var output = value.ToString(0+(new string('#',digits)),provider);
//确定显示的数字是多少(这部分不兼容文化)
var length = output.Length - output.IndexOf(。);
if(length< = digits){
return output;
} else {
return value.ToString(E+ format,provider);
}
}
}
这里是一个例子使用以下代码:
var tests = new [] {
0.0000055555,
0.00000555555555555555555,
};
var formatter = new DoubleFormatter();
foreach(var t in tests){
var result = string.Format(formatter,{0:EE15},t);
Console.WriteLine(result);
}
I have a problem with the conversion from double to string.
I want to convert:
double value: 0.0772486324655191
string value: 0.0772486324655191
and if the length is bigger than 16 digits after the decimal point I want it like this:
double value: 0.00063500244832493823
string value: 6.3500244832493823e-004
I have tried to convert it with IFormatProvider Pattern:
0.0000000000000000e000
But the result in the first case is
7.7248632465519100e-002
How can I get the number of digits in my double vector? Or better: how should I use the format provider correctly?
String specifier;
CultureInfo culture;
specifier = "0.0000000000000000e000";
culture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(DoubleMirrored[0].ToString(specifier, CultureInfo.InvariantCulture));
To do this, you definitely need to create a custom formatter.
To create a custom formatter, here's what you should know:
string.Format
has the following overload: string.Format(IFormatProvider, string, object[])
, so you must create a IFormatProvider
that will "provide" a ICustomFormatter
, which will handle your custom formatting. The same class can be easily used for both interfaces.
Here's some code that does exactly what you describe:
public class DoubleFormatter : IFormatProvider, ICustomFormatter
{
// Implementation of IFormatProvider:
public object GetFormat(Type t) {
if (t == typeof(ICustomFormatter)) {
return this;
}
return null;
}
// Implementation of ICustomFormatter:
public string Format(string format, object arg, IFormatProvider provider) {
// Search for the custom "EE" format specifier:
if (format == null || !format.StartsWith("EE")) return null;
format = format.Substring(2); // Trim "EE"
// Determine how many digits before we cutoff:
int digits;
if (!int.TryParse(format, out digits)) {
throw new FormatException("Format must contain digits");
}
// Get the value: (note, this will work for any numeric type)
var value = Convert.ToDouble(arg);
// Convert to string without using Exponential format:
var output = value.ToString("0."+(new string('#',digits)), provider);
// Determine how many digits are showing: (this part isn't culture-compatible)
var length = output.Length - output.IndexOf(".");
if (length <= digits) {
return output;
} else {
return value.ToString("E"+format, provider);
}
}
}
And here's an example of how to use this code:
var tests = new[]{
0.0000055555,
0.00000555555555555555555,
};
var formatter = new DoubleFormatter();
foreach (var t in tests){
var result = string.Format(formatter, "{0:EE15}", t);
Console.WriteLine(result);
}
这篇关于IFormatProvider从double到string - 数字数字的科学转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!