zoukankan      html  css  js  c++  java
  • [No0000164]C#,科学计数法的哽

    NewString = Decimal.Parse(OldString, System.Globalization.NumberStyles.Float).ToString();
    //Convert.ToDecimal(Decimal.Parse(OldString, System.Globalization.NumberStyles.Float)).ToString();
    public abstract class ScienceCount
        {
            public static string Exponent(double num)
            {
                double before =System.Math.Abs(num);
                int after=0;
                while (before >= 10 ||(before < 1&& before!=0))
                {
                    if (before >= 10)
                    {
                        before = before / 10;
                        after++;
                    }
                    else
                    {
                        before = before * 10;
                        after--;
                    }
                }
                return string.Concat(num>=0?"":"-",ReturnBefore(before),"E",ReturnAfter(after));
            }
            /// <summary>
            /// 有效数字的处理
            /// </summary>
            /// <param name="before">有效数字</param>
            /// <returns>三位有效数字,不足则补零</returns>
            public static string ReturnBefore(double before)
            {
                if (before.ToString() != null)
                {
                    char[] arr = before.ToString().ToCharArray();
                    switch (arr.Length)
                    {
                        case 1:
                        case 2: return string.Concat(arr[0] , "." , "00"); break;
                        case 3: return string.Concat(arr[0] + "." + arr[2] + "0"); break;
                        default: return string.Concat(arr[0] + "." + arr[2] + arr[3]); break;
                    }
                }
    
                return "000";
            }
            /// <summary>
            /// 幂的处理
            /// </summary>
            /// <param name="after">幂数</param>
            /// <returns>三位幂数部分,不足则补零</returns>
            public static string ReturnAfter(int after)
            {
                if (after.ToString() != null)
                {
                    string end;
                    char[] arr = System.Math.Abs(after).ToString().ToCharArray();
                    switch (arr.Length)
                    {
                        case 1: end = "00" + arr[0]; break;
                        case 2: end = "0" + arr[0] + arr[1]; break;
                        default: end = System.Math.Abs(after).ToString(); break;
                    }
                    return string.Concat(after >= 0 ? "+" : "-" , end);
                }
    
                return "+000";
            }
        }
    OldString = double.Parse(NewString).ToString("E");
    AllowCurrencySymbol 256

    Indicates that the numeric string can contain a currency symbol. Valid currency symbols are determined by the CurrencySymbol property.

    AllowDecimalPoint 32

    Indicates that the numeric string can have a decimal point. If the NumberStyles value includes the AllowCurrencySymbol flag and the parsed string includes a currency symbol, the decimal separator character is determined by the CurrencyDecimalSeparator property. Otherwise, the decimal separator character is determined by the NumberDecimalSeparator property.

    AllowExponent 128

    Indicates that the numeric string can be in exponential notation. The AllowExponent flag allows the parsed string to contain an exponent that begins with the "E" or "e" character and that is followed by an optional positive or negative sign and an integer. In other words, it successfully parses strings in the form nnnExxnnnE+xx, and nnnE-xx. It does not allow a decimal separator or sign in the significand or mantissa; to allow these elements in the string to be parsed, use the AllowDecimalPoint and AllowLeadingSign flags, or use a composite style that includes these individual flags.

    AllowHexSpecifier 512

    Indicates that the numeric string represents a hexadecimal value. Valid hexadecimal values include the numeric digits 0-9 and the hexadecimal digits A-F and a-f. Strings that are parsed using this style cannot be prefixed with "0x" or "&h". A string that is parsed with the AllowHexSpecifier style will always be interpreted as a hexadecimal value. The only flags that can be combined with AllowHexSpecifier are AllowLeadingWhite and AllowTrailingWhite. The NumberStyles enumeration includes a composite style, HexNumber, that consists of these three flags.

    AllowLeadingSign 4

    Indicates that the numeric string can have a leading sign. Valid leading sign characters are determined by the PositiveSign and NegativeSign properties.

    AllowLeadingWhite 1

    Indicates that leading white-space characters can be present in the parsed string. Valid white-space characters have the Unicode values U+0009, U+000A, U+000B, U+000C, U+000D, and U+0020. Note that this is a subset of the characters for which the IsWhiteSpace(Char) method returns true.

    AllowParentheses 16

    Indicates that the numeric string can have one pair of parentheses enclosing the number. The parentheses indicate that the string to be parsed represents a negative number.

    AllowThousands 64

    Indicates that the numeric string can have group separators, such as symbols that separate hundreds from thousands. If the NumberStyles value includes the AllowCurrencySymbol flag and the string to be parsed includes a currency symbol, the valid group separator character is determined by the CurrencyGroupSeparator property, and the number of digits in each group is determined by the CurrencyGroupSizes property. Otherwise, the valid group separator character is determined by the NumberGroupSeparator property, and the number of digits in each group is determined by the NumberGroupSizes property.

    AllowTrailingSign 8

    Indicates that the numeric string can have a trailing sign. Valid trailing sign characters are determined by the PositiveSign and NegativeSign properties.

    AllowTrailingWhite 2

    Indicates that trailing white-space characters can be present in the parsed string. Valid white-space characters have the Unicode values U+0009, U+000A, U+000B, U+000C, U+000D, and U+0020. Note that this is a subset of the characters for which the IsWhiteSpace(Char) method returns true.

    Any 511

    Indicates that all styles except AllowHexSpecifier are used. This is a composite number style.

    Currency 383

    Indicates that all styles except AllowExponent and AllowHexSpecifier are used. This is a composite number style.

    Float 167

    Indicates that the AllowLeadingWhiteAllowTrailingWhiteAllowLeadingSignAllowDecimalPoint, and AllowExponent styles are used. This is a composite number style.

    HexNumber 515

    Indicates that the AllowLeadingWhiteAllowTrailingWhite, and AllowHexSpecifier styles are used. This is a composite number style.

    Integer 7

    Indicates that the AllowLeadingWhiteAllowTrailingWhite, and AllowLeadingSign styles are used. This is a composite number style.

    None 0

    Indicates that no style elements, such as leading or trailing white space, thousands separators, or a decimal separator, can be present in the parsed string. The string to be parsed must consist of integral decimal digits only.

    Number 111

    Indicates that the AllowLeadingWhiteAllowTrailingWhiteAllowLeadingSignAllowTrailingSignAllowDecimalPoint, and AllowThousands styles are used. This is a composite number style.

  • 相关阅读:
    771. Jewels and Stones
    706. Design HashMap
    811. Subdomain Visit Count
    733. Flood Fill
    117. Populating Next Right Pointers in Each Node II
    250. Count Univalue Subtrees
    94. Binary Tree Inorder Traversal
    116. Populating Next Right Pointers in Each Node
    285. Inorder Successor in BST
    292. Nim Game Java Solutin
  • 原文地址:https://www.cnblogs.com/Chary/p/No0000164.html
Copyright © 2011-2022 走看看