Ejemplo n.º 1
0
        public override bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
        {
            var    text    = textField.Text;
            string newText = text;

            if (range.Location >= text.Length)
            {
                newText = text + replacementString;
            }
            else
            {
                var substr     = text.Substring(0, range.Location);
                var finalIndex = range.Location + range.Length - 1;
                if (finalIndex >= text.Length - 1)
                {
                    newText = substr + replacementString;
                }
                else
                {
                    var substr2 = text.Substring(finalIndex + 1);
                    newText = substr + replacementString + substr2;
                }
            }

            var numText = NumberFormattingHelper.StripCurrency(newText);

            if (numText.Length > _digitLimit)
            {
                return(false);
            }

            string result = string.Empty;

            if (numText.Length == 0)
            {
                result = "$0";
            }
            else
            {
                int number;
                if (Int32.TryParse(numText, System.Globalization.NumberStyles.Currency, System.Globalization.CultureInfo.CurrentCulture, out number))
                {
                    result = string.Format("{0:C0}", number);
                }
            }

            if (_postChangeAction != null)
            {
                _postChangeAction(result);
            }

            textField.Text = result;

            return(false);
        }
Ejemplo n.º 2
0
        private int?ParseResult(string text)
        {
            if (NumberFormattingHelper.IsEmptyValue(text))
            {
                return(0);
            }

            int result;

            if (Int32.TryParse(text, System.Globalization.NumberStyles.Currency, System.Globalization.CultureInfo.CurrentCulture, out result))
            {
                return(result);
            }
            return(null);
        }