Whether a string is a money amount, ie a currency + a number - CSharp System

CSharp examples for System:String Number

Description

Whether a string is a money amount, ie a currency + a number

Demo Code


using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Text;
using System.Net.Mail;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System;// www.  j  a  v a  2  s .c o m

public class Main{
        /// <summary>
        /// Whether a string is a money amount,
        /// ie a currency + a number
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsAmount(string input)
        {
            double number;
            var success = double.TryParse(input, NumberStyles.Currency,
                new CultureInfo("en-US"), out number);

            if (!success && Regex.IsMatch(input, CurrencyAndLetterPatterns))
            {
                // in this case, we have a precision in addition to the currency symbol; ex: US$1.6, C$5...
                var cleanedInput = Regex.Replace(input, CurrencyAndLetterPatterns, m => m.Groups[1].Value);
                return IsAmount(cleanedInput);
            }
            else
            {
                return success;
            }
        }
}

Related Tutorials