C#:如何将十进制转换为字符串而不转换为指数表示

例如,我有一个包含一些值的对象数组。它可以是十进制、双精度、整数或字符串。 object[] oo = {0.000278121, 0.000078125m, "Hello",1}; 例如,当将此值转换为字符串时,第二个值变为 7.8125E-05。但是第一个值保持在数组中。那么如何区分这些类型的解析逻辑,并以相同的格式解析所有十进制值呢?完整代码:


object[] oo = {0.000278121, 0.000078125,"Hello",1};

string[] ss = oo.Select(x => x.ToString()).ToArray();

Console.WriteLine(string.Join("|",ss)); // 0.000278121|7.8125E-05


暮色呼如
浏览 191回答 4
4回答

慕姐8265434

首先,这不是一个decimal,而是一个double。0.000278121m, 0.000078125m如果你想要decimals,请使用。要强制使用没有指数的全长字符串表示形式,请使用.ToString("0.#################")double。使用小数,默认情况下会这样做,.ToString()因为您可以正常工作。

三国纷争

如果您有不同的对象集合,请区别对待每种类型:var outStrings = new List<string>();object[] oo = { 0.000278121, 0.000078125, "Hello World" };foreach (var ooItem in oo) {&nbsp; &nbsp; if (ooItem is double dOo) {&nbsp; &nbsp; &nbsp; &nbsp; outStrings.Add(dOo.ToString("0.#################"));&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; outStrings.Add(ooItem.ToString());&nbsp; &nbsp; }}如果您有许多要单独处理的类型,请使用模式匹配switch语句

森栏

我相信你可以使用 double.ToString(string) 方法。请参阅: https ://docs.microsoft.com/en-us/dotnet/api/system.double.tostring?view=netframework-4.7.2#System_Double_ToString_System_String_该参数可以是自定义数字格式字符串 ( https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings ) 或标准数字格式字符串 ( https:// docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings?view=netframework-4.7.2#NFormatString)。“N”格式说明符的精度说明符应在 0 到 99 之间(N0 ~ N99),否则 ToString 方法将返回作为参数传递的字符串。你可以这样做double.ToString("N99"),它将以最高精度返回数字形式的字符串。这样做的问题是,如果您这样做0.000078125.ToString("N99"),输出将有很多尾随 0,例如:0.0000781250000000000...。为了克服这个问题,您可以使用 string.TrimEnd(char) 方法。0.000078125.ToString("N99").TrimEnd('0') 将修剪尾随零,因此输出可以是 0.000078125。在给定的示例代码中,这可以应用为://Avoid boxing - specify types whenever possible, or use genericdouble[] oo = {0.000278121, 0.000078125};string[] ss = oo.Select(x => x.ToString("N99").TrimEnd('0')).ToArray();Console.WriteLine(string.Join("|",ss)); // 0.000278121|0.000078125编辑:我应该更仔细地阅读问题,更改类型oo不适合您的情况。但是,总体思路是一样的;将对象转换为双精度,使用具有适当精度的 ToString(string) 方法,然后修剪尾随零。您可以通过执行来检查对象的类型是否为双精度,此外,通过(模式匹配)obj is double将其转换回双精度。obj is double dbl编辑2:public static IEnumerable<string> AllToString(IEnumerable<object> objArray){&nbsp; &nbsp; foreach (object obj in objArray)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; switch (obj)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case double dbl:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return dbl.ToString("N99").TrimEnd('0');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case bool b:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return b.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case int i:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return i.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case short s:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return s.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case string str:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return str;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case float flt:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return flt.ToString("N99").TrimEnd('0');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case decimal dcm:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return dcm.ToString("N99").TrimEnd('0');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case byte bt:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return bt.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case char ch:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return ch.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case uint ui:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return ui.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ushort us:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return us.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case sbyte sb:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return sb.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case long ln:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return ln.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ulong uln:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return uln.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case null:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return null; //new string()?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case DateTime dt:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return dt.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case TimeSpan ts:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return ts.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default: //Fallback&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return obj.ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}将对象数组传递给方法,然后根据需要调用 .ToArray() 或 .ToList()。这会将对象数组/列表中的每个项目转换为字符串集合。

翻翻过去那场雪

而不是下面的行:string[]&nbsp;ss&nbsp;=&nbsp;oo.Select(x&nbsp;=>&nbsp;x.ToString()).ToArray();您可以使用下面的代码行:我假设 10 是您在输入集中可以拥有的最大十进制位数。如果有更多,则将 N10 更改为其他数字。string[]&nbsp;ss&nbsp;=&nbsp;oo.Select(x&nbsp;=>&nbsp;((double)x).ToString("N10")).ToArray();
打开App,查看更多内容
随时随地看视频慕课网APP