System.Number.ParseSingle C# (CSharp) Method

ParseSingle() static private method

static private ParseSingle ( String value, NumberStyles options, NumberFormatInfo numfmt ) : Single
value String
options NumberStyles
numfmt NumberFormatInfo
return Single
        internal unsafe static Single ParseSingle(String value, NumberStyles options, NumberFormatInfo numfmt) {

            Byte * numberBufferBytes = stackalloc Byte[NumberBuffer.NumberBufferBytes];
            NumberBuffer number = new NumberBuffer(numberBufferBytes);
            Double d = 0;

            StringToNumber(value, options, ref number, numfmt, false);

            if (!NumberBufferToDouble(number.PackForNative(), ref d)) {
                throw new OverflowException(Environment.GetResourceString("Overflow_Single"));
            }
            Single castSingle = (Single)d;
            if (Single.IsInfinity(castSingle)) {
                throw new OverflowException(Environment.GetResourceString("Overflow_Single"));
            }
            return castSingle;
        }

Usage Example

示例#1
0
 // Parses a float from a String in the given style.  If
 // a NumberFormatInfo isn't specified, the current culture's
 // NumberFormatInfo is assumed.
 //
 // This method will not throw an OverflowException, but will return
 // PositiveInfinity or NegativeInfinity for a number that is too
 // large or too small.
 //
 //| <include path='docs/doc[@for="Single.Parse3"]/*' />
 public static float Parse(String s, NumberStyles style)
 {
     try {
         return(Number.ParseSingle(s, style));
     }
     catch (FormatException) {
         //If we caught a FormatException, it may be from one of our special strings.
         //Check the three with which we're concerned and rethrow if it's not one of
         //those strings.
         String sTrim = s.Trim();
         if (sTrim.Equals(NumberFormatInfo.positiveInfinitySymbol))
         {
             return(PositiveInfinity);
         }
         if (sTrim.Equals(NumberFormatInfo.negativeInfinitySymbol))
         {
             return(NegativeInfinity);
         }
         if (sTrim.Equals(NumberFormatInfo.nanSymbol))
         {
             return(NaN);
         }
         //Rethrow the previous exception;
         throw;
     }
 }
All Usage Examples Of System.Number::ParseSingle