如何转换包含一个指数为十进制和回串一串 [英] How to convert a string containing an exponential number to decimal and back to string

查看:193
本文介绍了如何转换包含一个指数为十进制和回串一串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将code Delphi和C#之间。
值存储在从Delphi应用程序中的文本文件中的字符串。所存储的值的一个例子是:4.42615029219009E-5'

I'm converting code between delphi and c#.
Values are stored as strings in a text file from the delphi app. An example of the stored value is : '4.42615029219009E-5'

现在在我的C#应用​​程序,我需要读取该字符串值,然后以后必须重新写出来的价值的能力。起初我用code类似于:

Now in my c# app I need to read in that string value and then later have the capability to write out the value again. Initially I used code similar to:

string stringField = "4.42615029219009E-5";
double someMoneyVar = Convert.ToDouble(stringField)

之后,如果我需要重新创建与someMoneyVar然后用一个简单的值的文本文件:

later if I need to recreate the text file with the value of someMoneyVar then using a simple:

string.Format("{0}", someMoneyVar)

将输出:

4.42615029219009E-05 // note the 0

最后,我读到最好是钱存放在C#小数。我试图将字符串值转换为使用 decimal.Parse(someMoneyVar,NumberStyles.Any),但格式会丢失小数。

Lastly, I read that it is better to store money as decimals in c#. I've tried to convert the string value to a decimal using decimal.Parse(someMoneyVar, NumberStyles.Any) , however the formatting is lost.

我需要的要输出的数据,正是因为它是输入。

请注意,someMoneyVar的值可能不总是含有一个指数部分。例如0.0428860331919443。如果没有指数部分到someMoneyVar然后该值被正确地写入到文本文件中。的值

Note, the value of someMoneyVar may not always contain an exponential part. e.g. 0.0428860331919443. If there is no exponential part to the value of someMoneyVar then the value is written correctly to the text file.

更新:
挖掘到德尔福的 FloatToStr 功能和帮助文件(这是存储在文本文件中的值),我带着以下内容:

Update:
Digging into delphi's FloatToStr function and help files (which is what stores the value in the text file) i came with the following:

将所得的字符串使用定点格式如果位数   在值的小数点的左边是小于或等于   到指定precision,并且如果该值大于或等于   0.00001 (编辑:这应该是0.0001有delphi的文档中的错误)。否则,所产生的字符串使用科学格式,   和数字参数指定的位数的最小数目   指数(0和4之间)。
  ...
  如果截面为正值是空的,或如果整个格式   字符串是空的,该值是使用普通浮点格式   15显著数字格式,对应调用   FloatToStrF与 ffGeneral格式。一般浮点   格式也可以用来当值有超过18位的   左小数点和格式字符串不指定   科学记数法。

The resulting string uses fixed point format if the number of digits to the left of the decimal point in the value is less than or equal to the specified precision, and if the value is greater than or equal to 0.00001 ( this should be 0.0001. There is an error in the delphi documentation). Otherwise the resulting string uses scientific format, and the Digits parameter specifies the minimum number of digits in the exponent (between 0 and 4).
...
If the section for positive values is empty, or if the entire format string is empty, the value is formatted using general floating-point formatting with 15 significant digits, corresponding to a call to FloatToStrF with the ffGeneral format. General floating-point formatting is also used if the value has more than 18 digits to the left of the decimal point and the format string does not specify scientific notation.

因此​​,在铭记在 FloatToStr 函数的功能调用的 FloatToStrF 使用15显著(precision)数字和0的数字的最少数量,因此我们结束了

So bearing in mind that the FloatToStr function does a call to FloatToStrF uses 15 significant (precision) digits and a 0 as the minumum number of digits hence we end up with

4.42615029219009E-5

4.42615029219009E-5

如果数字是2,那么该数字将显示为

if the digits was 2 then the number would be displayed as

4.42615029219009E-05

4.42615029219009E-05

根据MSDN的 http://msdn.microsoft.com/ EN-US /库/ dwhawy9k.aspx#GFormatString
使用通用格式说明,双重的precision为15,小数为29

According to the MSDN http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#GFormatString
using the general format specifier, the precision of double is 15 and decimal is 29

定点表示法用于如果指数,将导致从   EX pressing科学记数法的数大于-5   小于precision符;否则,科学记数法是   使用。结果包含一个小数点如果需要的话,和后   小数点后的零被省略。如果precision说明   是present和显著的位数在结果超过   指定precision,多余的尾随位被删除   四舍五入。

Fixed-point notation is used if the exponent that would result from expressing the number in scientific notation is greater than -5 and less than the precision specifier; otherwise, scientific notation is used. The result contains a decimal point if required, and trailing zeros after the decimal point are omitted. If the precision specifier is present and the number of significant digits in the result exceeds the specified precision, the excess trailing digits are removed by rounding.

但是,如果数字是一个小数和precision说明符   省略,定点符号总是使用和尾随零   preserved。

However, if the number is a Decimal and the precision specifier is omitted, fixed-point notation is always used and trailing zeros are preserved.

如果科学记数法时,在结果指数是prefixed   以E如果格式说明符是G或E如果格式   说明符为G。 该指数包含至少两位。本   不同于由产生用于科学记数法的格式   指数格式说明,其中包括至少三个   数字的指数。

If scientific notation is used, the exponent in the result is prefixed with "E" if the format specifier is "G", or "e" if the format specifier is "g". The exponent contains a minimum of two digits. This differs from the format for scientific notation that is produced by the exponential format specifier, which includes a minimum of three digits in the exponent.

该结果字符串受的格式化信息   当前NumberFormatInfo对象。下表列出了   的NumberFormatInfo属性控制结果的格式   字符串。

The result string is affected by the formatting information of the current NumberFormatInfo object. The following table lists the NumberFormatInfo properties that control the formatting of the result string.

我们可以很容易地设置precision例如mydecimal.toString(G15),但是我还没有找到一种方法,因为很容易设置的位数'E'符号后面的数字是在Delphi的 FloatToStrF 功能

One can easily set the precision e.g. mydecimal.toString("G15") however i still haven't found a way to set the number of digits after the 'E' sign as easily as in the delphi FloatToStrF function

推荐答案

要字符串转换为数字,因为你已经想通了,你只需要使用。 我想尝试不同的转换,但:

To convert strings to numbers, as you already figured out, you just use a double. I'd try a different conversion though:

double myNum = double.Parse("<yournumber>", NumberStyles.AllowExponent | NumberStyles.Float, CultureInfo.InvariantCulture);

AllowExponent 浮动应保持标记和 InvariantCulture的负责小数分频器的(这可能不是一个点取决于区域设置)。

AllowExponent and Float should keep the notation, and InvariantCulture takes care of the decimal divider (which might not be a dot depending on the locale).

您可以输出的科学记数法表示通过的String.Format(),像这样的:

You can output scientific notation numbers via string.Format(), like this:

double num = 1234.5678; // 1.2345678e+03
string.Format("{0:e}", num); // should output "1.2345678E+03"

如果您有使用或不使用E + XX的部件编号来区分,你就必须转换成字符串翻番之前,搜索它,和一个完整的片段(警告:未测试)可能看起来像

If you have to distinguish between numbers with and without the "E+xx" part, you'll have to search for it before converting the string to double, and a full snippet (WARNING: not tested!) could look like:

string myString = ReadNumberFromFile(); // Just a placeholder method
double myNum = double.Parse(myString, NumberStyles.AllowExponent | NumberStyles.Float, CultureInfo.InvariantCulture);
string output = string.Empty; //this will be the "converted-back number" container
if (myString.IndexOf("e", StringComparison.OrdinalIgnoreCase) >= 0)
{
    //Number contains the exponent
    output = string.Format("{0:e}", num); // exponential notation 'xxxExx' casing of 'e' changes the casing of the 'e' in the string
}
else
{
    //TODO: Number does NOT contain the exponent
    output = string.Format("{0:f}", num); // fixed-point notation in the form 'xxxx.xxx'
}

这里的关键是,尽可能数去,为的没有的指数不作任何任何区别,这是再$只是一个问题p $ psentation(并没有什么意义来区分他们:这是真的的同样的事情的)。

The point here is that, as far as number go, being with or without an exponent doesn't make any difference whatsoever, it's just a matter of representation (and it makes little sense to distinguish between them: it's really the same thing).

这篇关于如何转换包含一个指数为十进制和回串一串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆