if the string can be parse as Double respective Int32 Spaces are not considered. - CSharp System

CSharp examples for System:Double

Description

if the string can be parse as Double respective Int32 Spaces are not considered.

Demo Code


using System.Threading;
using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;/*from  ww  w. ja  va 2s.  c  om*/

public class Main{
        /// <summary>
        /// true, if the string can be parse as Double respective Int32
        /// Spaces are not considered.
        /// </summary>
        /// <param name="s">input string</param>

        /// <param name="floatpoint">true, if Double is considered,
        /// otherwhise Int32 is considered.</param>
        /// <returns>true, if the string contains only digits or float-point</returns>
        public static bool IsNumber(string s, bool floatpoint)
        {
            int i;
            double d;
            string withoutWhiteSpace = s.Replace(" ", string.Empty);
            if (floatpoint)
                return double.TryParse(withoutWhiteSpace, NumberStyles.Any,
                    Thread.CurrentThread.CurrentUICulture, out d);
            else
                return int.TryParse(withoutWhiteSpace, out i);
        }
}

Related Tutorials