IFormatProvider从double到string的科学转换

编程入门 行业动态 更新时间:2024-10-28 20:17:44
IFormatProvider从double到string的科学转换 - 位数(IFormatProvider scientific conversion from double to string - number of digits)

我有从double到string的转换问题。

我要转换:

double值:0.0772486324655191

字符串值:0.0772486324655191

如果长度大于小数点后的16位数,我希望它像这样:

double值:0.00063500244832493823

字符串值:6.3500244832493823e-004

我试图用IFormatProvider模式转换它:

0.0000000000000000e000

但第一种情况的结果是

7.7248632465519100e-002

如何获取双向量中的位数? 或者更好:我应该如何正确使用格式提供程序?

String specifier; CultureInfo culture; specifier = "0.0000000000000000e000"; culture = CultureInfo.CreateSpecificCulture("en-US"); Console.WriteLine(DoubleMirrored[0].ToString(specifier, CultureInfo.InvariantCulture));

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));

最满意答案

为此,您肯定需要创建自定义格式化程序。

要创建自定义格式化程序,您应该知道: string.Format具有以下重载: string.Format(IFormatProvider, string, object[]) ,因此您必须创建一个“提供” ICustomFormatter的IFormatProvider ,它将处理您的自定义格式。 两个接口都可以轻松使用相同的类。

这里有一些代码完全符合您的描述:

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); } } }

以下是如何使用此代码的示例:

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); }

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); }

更多推荐

本文发布于:2023-07-14 17:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:科学   IFormatProvider   double   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!