Example #1
0
        public static Amount Parse(string text)
        {
            if (text == null)
            {
                return(Amount.Zero);
            }
            Single value = 1;

            // see if text starts with a single value
            int pos = text.IndexOf(" ");

            if (pos < 0)
            {
                if (text.EndsWith("g"))
                {
                    if (text.Length > 1)
                    {
                        if (char.IsDigit(text[text.Length - 2]))
                        {
                            text = text.Replace("g", " g");
                            pos  = text.IndexOf(" ");
                        }
                    }
                }

                if (text.EndsWith("ml"))
                {
                    if (text.Length > 2)
                    {
                        if (char.IsDigit(text[text.Length - 3]))
                        {
                            text = text.Replace("ml", " ml");
                            pos  = text.IndexOf(" ");
                        }
                    }
                }
            }

            if (pos > 0)
            {
                value = Floats.ParseUnknown(text.Substring(0, pos), -989898);
                if (value == -989898)
                {
                    value = 1;
                }
                else
                {
                    text = text.Substring(pos + 1);
                }
            }

            //string SubAmount = null;
            pos = text.IndexOf("(");
            if (pos > -1)
            {
                if (text.LastIndexOf(")") < pos)
                {
                    text = text.Replace("(", "").Replace(")", "");
                }
            }
            //else
            //{
            //    var ext = text.Substring(pos, text.LastIndexOf(")") - pos + 1);
            //    text = text.Replace(ext, "").Trim();
            //    SubAmount = ext.Replace("(", "").Replace(")", "");
            //    if (SubAmount.Length == 0) SubAmount = null;
            //    if (((Amount)SubAmount).ToString() == "1 ")
            //    {
            //        //System.Diagnostics.Debug.WriteLine("Invalid SubAmount: " + SubAmount);
            //        SubAmount = null;
            //    }
            //}

            // text now contains only unit, see if its standard, if not, assign to nonStd
            Unit stdUnit = Unit.Parse(text);

            if (stdUnit == null)
            {
                return(new Amount(value, text));
            }

            return(new Amount(value, stdUnit));
        }