示例#1
0
        // 检查订购价是否合法。注意,这是检查一个单个的订购价,即,内容中不允许包含 [] 符号
        // return:
        //      -1  校验过程出错
        //      0   校验正确
        //      1   校验发现错误
        public static int VerifyOrderPrice(string strPrice, out string strError)
        {
            strError = "";
            if (string.IsNullOrEmpty(strPrice))
            {
                return(1);
            }
            if (strPrice.IndexOfAny(new char[] { '[', ']' }) != -1)
            {
                strError = "校验价格字符串 '" + strPrice + "' 时出错:不应包含 [] 字符";
                return(-1);
            }

            try
            {
                CurrencyItem item = CurrencyItem.Parse(strPrice);
            }
            catch (Exception ex)
            {
                strError = "价格字符串 '" + strPrice + "' 不合法:" + ex.Message;
                return(1);
            }

            return(0);
        }
示例#2
0
        // 判断两个价格字符串是否相等
        public static bool IsEqual(string strPrice1, string strPrice2, string strDefaultPrefix = "CNY")
        {
            if (strPrice1 == strPrice2)
            {
                return(true);
            }

            try
            {
                CurrencyItem item1 = CurrencyItem.Parse(strPrice1);
                CurrencyItem item2 = CurrencyItem.Parse(strPrice2);
                if (string.IsNullOrEmpty(item1.Prefix))
                {
                    item1.Prefix = strDefaultPrefix;
                }
                if (string.IsNullOrEmpty(item2.Prefix))
                {
                    item2.Prefix = strDefaultPrefix;
                }
                if (item1.Prefix == item2.Prefix &&
                    item1.Value == item2.Value &&
                    item1.Postfix == item2.Postfix)
                {
                    return(true);
                }
                return(false);
            }
            catch
            {
                return(false);
            }
        }
示例#3
0
        // 看看若干个价格字符串是否都表示了0?
        // return:
        //      -1  出错
        //      0   不为0
        //      1   为0
        public static int IsZero(List <string> prices,
                                 out string strError)
        {
            strError = "";

            List <CurrencyItem> items = new List <CurrencyItem>();

            // 变换为PriceItem
            for (int i = 0; i < prices.Count; i++)
            {
                string strPrefix  = "";
                string strValue   = "";
                string strPostfix = "";
                int    nRet       = ParsePriceUnit(prices[i],
                                                   out strPrefix,
                                                   out strValue,
                                                   out strPostfix,
                                                   out strError);
                if (nRet == -1)
                {
                    return(-1);
                }
                decimal value = 0;
                try
                {
                    value = Convert.ToDecimal(strValue);
                }
                catch
                {
                    strError = "数字 '" + strValue + "' 格式不正确";
                    return(-1);
                }

                CurrencyItem item = new CurrencyItem();
                item.Prefix  = strPrefix;
                item.Postfix = strPostfix;
                item.Value   = value;

                items.Add(item);
            }

            // 分析
            for (int i = 0; i < items.Count; i++)
            {
                CurrencyItem item = items[i];

                if (item.Value != 0)
                {
                    return(0);   // 中间出现了不为0的
                }
            }

            return(1);   // 全部为0
        }
示例#4
0
        public static CurrencyItem Parse(string strText)
        {
            string strError = "";

            string strPrefix  = "";
            string strValue   = "";
            string strPostfix = "";
            int    nRet       = PriceUtil.ParsePriceUnit(strText,
                                                         out strPrefix,
                                                         out strValue,
                                                         out strPostfix,
                                                         out strError);

            if (nRet == -1)
            {
                throw new Exception(strError);
            }

            decimal value = 0;

            try
            {
                value = Convert.ToDecimal(strValue);
            }
            catch
            {
                strError = "数字 '" + strValue + "' 格式不正确";
                throw new Exception(strError);
            }

            CurrencyItem item = new CurrencyItem();

            item.Prefix  = strPrefix;
            item.Postfix = strPostfix;
            item.Value   = value;

            return(item);
        }
示例#5
0
        // 汇总价格
        // 货币单位不同的,互相独立
        // parameters:
        //      prices  若干单一价格字符串构成的数组。并未进行过排序
        // return:
        //      -1  error
        //      0   succeed
        public static int TotalPrice(List <string> prices,
                                     out List <string> results,
                                     out string strError)
        {
            strError = "";
            results  = new List <string>();

            List <CurrencyItem> items = new List <CurrencyItem>();

            // 变换为PriceItem
            // for (int i = 0; i < prices.Count; i++)
            foreach (string price in prices)
            {
                // string strText = prices[i].Trim();
                if (price == null)
                {
                    continue;
                }
                string strText = price.Trim();

                if (String.IsNullOrEmpty(strText) == true)
                {
                    continue;
                }

#if NO
                string strLeft     = "";
                string strRight    = "";
                string strOperator = "";
                // 先处理乘除号
                // return:
                //      -1  出错
                //      0   没有发现乘号、除号
                //      1   发现乘号或者除号
                int nRet = ParseMultipcation(strText,
                                             out strLeft,
                                             out strRight,
                                             out strOperator,
                                             out strError);
                if (nRet == -1)
                {
                    return(-1);
                }

                if (nRet == 1)
                {
                    Debug.Assert(strOperator.Length == 1, "");

                    if (String.IsNullOrEmpty(strLeft) == true ||
                        String.IsNullOrEmpty(strRight) == true)
                    {
                        strError = "金额字符串格式错误 '" + strText + "'。乘号或除号的两边必须都有内容";
                        return(-1);
                    }
                }


                string strPrice    = "";
                string strMultiper = "";

                if (nRet == 0)
                {
                    Debug.Assert(String.IsNullOrEmpty(strLeft) == true, "");
                    Debug.Assert(String.IsNullOrEmpty(strRight) == true, "");
                    Debug.Assert(String.IsNullOrEmpty(strOperator) == true, "");

                    strPrice = strText.Trim();
                }
                else
                {
                    Debug.Assert(nRet == 1, "");

                    if (StringUtil.IsDouble(strLeft) == false &&
                        StringUtil.IsDouble(strRight) == false)
                    {
                        strError = "金额字符串格式错误 '" + strText + "'。乘号或除号的两边必须至少有一边是纯数字";
                        return(-1);
                    }


                    if (StringUtil.IsDouble(strLeft) == false)
                    {
                        strPrice    = strLeft;
                        strMultiper = strRight;
                    }
                    else if (StringUtil.IsDouble(strRight) == false)
                    {
                        strPrice    = strRight;
                        strMultiper = strLeft;
                        if (strOperator == "/")
                        {
                            strError = "金额字符串格式错误 '" + strText + "'。除号的右边才能是纯数字";
                            return(-1);
                        }
                    }
                    else
                    {
                        // 默认左边是价格,右边是倍率
                        strPrice    = strLeft;
                        strMultiper = strRight;
                    }
                }

                string strPrefix  = "";
                string strValue   = "";
                string strPostfix = "";
                nRet = ParsePriceUnit(strPrice,
                                      out strPrefix,
                                      out strValue,
                                      out strPostfix,
                                      out strError);
                if (nRet == -1)
                {
                    return(-1);
                }

                // 2012/1/5
                if (string.IsNullOrEmpty(strValue) == true)
                {
                    strError = "单个金额字符串 '" + strPrice + "' 中没有包含数字部分";
                    return(-1);
                }

                decimal value = 0;
                try
                {
                    value = Convert.ToDecimal(strValue);
                }
                catch
                {
                    strError = "单个金额字符串 '" + strPrice + "' 中, 数字部分 '" + strValue + "' 格式不正确";
                    return(-1);
                }

                if (String.IsNullOrEmpty(strOperator) == false)
                {
                    double multiper = 0;
                    try
                    {
                        multiper = Convert.ToDouble(strMultiper);
                    }
                    catch
                    {
                        strError = "数字 '" + strMultiper + "' 格式不正确";
                        return(-1);
                    }

                    if (strOperator == "*")
                    {
                        value = (decimal)((double)value * multiper);
                    }
                    else
                    {
                        Debug.Assert(strOperator == "/", "");

                        if (multiper == 0)
                        {
                            strError = "金额字符串格式错误 '" + strText + "'。除法运算中,除数不能为0";
                            return(-1);
                        }

                        value = (decimal)((double)value / multiper);
                    }
                }

                PriceItem item = new PriceItem();
                item.Prefix  = strPrefix.ToUpper();
                item.Postfix = strPostfix.ToUpper();
                item.Value   = value;

                // 缺省货币为人民币
                if (item.Prefix == "" && item.Postfix == "")
                {
                    item.Prefix = "CNY";
                }
#endif
                CurrencyItem item = null;
                int          nRet = ParseSinglePrice(strText,
                                                     out item,
                                                     out strError);
                if (nRet == -1)
                {
                    return(-1);
                }

                items.Add(item);
            }

            // 汇总
            for (int i = 0; i < items.Count; i++)
            {
                CurrencyItem item = items[i];

                for (int j = i + 1; j < items.Count; j++)
                {
                    CurrencyItem current_item = items[j];
                    if (current_item.Prefix == item.Prefix &&
                        current_item.Postfix == item.Postfix)
                    {
                        item.Value += current_item.Value;
                        items.RemoveAt(j);
                        j--;
                    }

                    /*
                     * else
                     * break;
                     * */
                    // 这里是一个BUG。没有排序,并不知道后面还有没有重复的事项呢,不能break。2009/10/10 changed
                }
            }

            // 输出
            for (int i = 0; i < items.Count; i++)
            {
                CurrencyItem item  = items[i];
                decimal      value = item.Value;

                string fmt = "0.00";    // #.##

                // 负号要放在最前面
                if (value < 0)
                {
                    results.Add("-" + item.Prefix + (-value).ToString(fmt) + item.Postfix);
                }
                else
                {
                    results.Add(item.Prefix + value.ToString(fmt) + item.Postfix);
                }
            }

            // 注: value.ToString("#.##") 采用的是四舍五入的方法
            return(0);
        }
示例#6
0
        // 解析单个金额字符串。例如 CNY10.00 或 -CNY100.00/7
        public static int ParseSinglePrice(string strText,
                                           out CurrencyItem item,
                                           out string strError)
        {
            strError = "";
            item     = new CurrencyItem();

            if (string.IsNullOrEmpty(strText) == true)
            {
                return(0);
            }

            strText = strText.Trim();

            if (String.IsNullOrEmpty(strText) == true)
            {
                return(0);
            }

            string strLeft     = "";
            string strRight    = "";
            string strOperator = "";
            // 先处理乘除号
            // return:
            //      -1  出错
            //      0   没有发现乘号、除号
            //      1   发现乘号或者除号
            int nRet = ParseMultipcation(strText,
                                         out strLeft,
                                         out strRight,
                                         out strOperator,
                                         out strError);

            if (nRet == -1)
            {
                return(-1);
            }

            if (nRet == 1)
            {
                Debug.Assert(strOperator.Length == 1, "");

                if (String.IsNullOrEmpty(strLeft) == true ||
                    String.IsNullOrEmpty(strRight) == true)
                {
                    strError = "金额字符串格式错误 '" + strText + "'。乘号或除号的两边必须都有内容";
                    return(-1);
                }
            }

            string strPrice    = "";
            string strMultiper = "";

            if (nRet == 0)
            {
                Debug.Assert(String.IsNullOrEmpty(strLeft) == true, "");
                Debug.Assert(String.IsNullOrEmpty(strRight) == true, "");
                Debug.Assert(String.IsNullOrEmpty(strOperator) == true, "");

                strPrice = strText.Trim();
            }
            else
            {
                Debug.Assert(nRet == 1, "");

                if (StringUtil.IsDouble(strLeft) == false &&
                    StringUtil.IsDouble(strRight) == false)
                {
                    strError = "金额字符串格式错误 '" + strText + "'。乘号或除号的两边必须至少有一边是纯数字";
                    return(-1);
                }

                if (StringUtil.IsDouble(strLeft) == false)
                {
                    strPrice    = strLeft;
                    strMultiper = strRight;
                }
                else if (StringUtil.IsDouble(strRight) == false)
                {
                    strPrice    = strRight;
                    strMultiper = strLeft;
                    if (strOperator == "/")
                    {
                        strError = "金额字符串格式错误 '" + strText + "'。除号的右边才能是纯数字";
                        return(-1);
                    }
                }
                else
                {
                    // 默认左边是价格,右边是倍率
                    strPrice    = strLeft;
                    strMultiper = strRight;
                }
            }

            string strPrefix  = "";
            string strValue   = "";
            string strPostfix = "";

            nRet = ParsePriceUnit(strPrice,
                                  out strPrefix,
                                  out strValue,
                                  out strPostfix,
                                  out strError);
            if (nRet == -1)
            {
                return(-1);
            }

            if (string.IsNullOrEmpty(strValue) == true)
            {
                strError = "金额字符串 '" + strPrice + "' 中没有包含数字部分";
                return(-1);
            }

            decimal value = 0;

            try
            {
                value = Convert.ToDecimal(strValue);
            }
            catch
            {
                strError = "金额字符串 '" + strPrice + "' 中, 数字部分 '" + strValue + "' 格式不正确";
                return(-1);
            }

            if (String.IsNullOrEmpty(strOperator) == false)
            {
                double multiper = 0;
                try
                {
                    multiper = Convert.ToDouble(strMultiper);
                }
                catch
                {
                    strError = "数字 '" + strMultiper + "' 格式不正确";
                    return(-1);
                }

                if (strOperator == "*")
                {
                    value = (decimal)((double)value * multiper);
                }
                else
                {
                    Debug.Assert(strOperator == "/", "");

                    if (multiper == 0)
                    {
                        strError = "金额字符串格式错误 '" + strText + "'。除法运算中,除数不能为0";
                        return(-1);
                    }

                    // value = (decimal)((double)value / multiper);
                    value = Convert.ToDecimal((double)value / multiper);
                }
            }

            item.Prefix  = strPrefix.ToUpper();
            item.Postfix = strPostfix.ToUpper();
            item.Value   = value;

            // 缺省货币为人民币
            if (item.Prefix == "" && item.Postfix == "")
            {
                item.Prefix = "CNY";
            }

            return(0);
        }
示例#7
0
        // ������������ַ��������� CNY10.00 �� -CNY100.00/7
        public static int ParseSinglePrice(string strText,
            out CurrencyItem item,
            out string strError)
        {
            strError = "";
            item = new CurrencyItem();

            if (string.IsNullOrEmpty(strText) == true)
                return 0;

            strText = strText.Trim();

            if (String.IsNullOrEmpty(strText) == true)
                return 0;

            string strLeft = "";
            string strRight = "";
            string strOperator = "";
            // �ȴ���˳���
            // return:
            //      -1  ����
            //      0   û�з��ֳ˺š�����
            //      1   ���ֳ˺Ż��߳���
            int nRet = ParseMultipcation(strText,
                out strLeft,
                out strRight,
                out strOperator,
                out strError);
            if (nRet == -1)
                return -1;

            if (nRet == 1)
            {
                Debug.Assert(strOperator.Length == 1, "");

                if (String.IsNullOrEmpty(strLeft) == true
                    || String.IsNullOrEmpty(strRight) == true)
                {
                    strError = "����ַ�����ʽ���� '" + strText + "'���˺Ż���ŵ����߱��붼������";
                    return -1;
                }
            }

            string strPrice = "";
            string strMultiper = "";

            if (nRet == 0)
            {
                Debug.Assert(String.IsNullOrEmpty(strLeft) == true, "");
                Debug.Assert(String.IsNullOrEmpty(strRight) == true, "");
                Debug.Assert(String.IsNullOrEmpty(strOperator) == true, "");

                strPrice = strText.Trim();
            }
            else
            {
                Debug.Assert(nRet == 1, "");

                if (StringUtil.IsDouble(strLeft) == false
                    && StringUtil.IsDouble(strRight) == false)
                {
                    strError = "����ַ�����ʽ���� '" + strText + "'���˺Ż���ŵ����߱���������һ���Ǵ�����";
                    return -1;
                }

                if (StringUtil.IsDouble(strLeft) == false)
                {
                    strPrice = strLeft;
                    strMultiper = strRight;
                }
                else if (StringUtil.IsDouble(strRight) == false)
                {
                    strPrice = strRight;
                    strMultiper = strLeft;
                    if (strOperator == "/")
                    {
                        strError = "����ַ�����ʽ���� '" + strText + "'�����ŵ��ұ߲����Ǵ�����";
                        return -1;
                    }
                }
                else
                {
                    // Ĭ������Ǽ۸��ұ��DZ���
                    strPrice = strLeft;
                    strMultiper = strRight;
                }
            }

            string strPrefix = "";
            string strValue = "";
            string strPostfix = "";
            nRet = ParsePriceUnit(strPrice,
                out strPrefix,
                out strValue,
                out strPostfix,
                out strError);
            if (nRet == -1)
                return -1;

            if (string.IsNullOrEmpty(strValue) == true)
            {
                strError = "����ַ��� '" + strPrice + "' ��û�а������ֲ���";
                return -1;
            }

            decimal value = 0;
            try
            {
                value = Convert.ToDecimal(strValue);
            }
            catch
            {
                strError = "����ַ��� '" + strPrice + "' ��, ���ֲ��� '" + strValue + "' ��ʽ����ȷ";
                return -1;
            }

            if (String.IsNullOrEmpty(strOperator) == false)
            {
                double multiper = 0;
                try
                {
                    multiper = Convert.ToDouble(strMultiper);
                }
                catch
                {
                    strError = "���� '" + strMultiper + "' ��ʽ����ȷ";
                    return -1;
                }

                if (strOperator == "*")
                {
                    value = (decimal)((double)value * multiper);
                }
                else
                {
                    Debug.Assert(strOperator == "/", "");

                    if (multiper == 0)
                    {
                        strError = "����ַ�����ʽ���� '" + strText + "'�����������У���������Ϊ0";
                        return -1;
                    }

                    value = (decimal)((double)value / multiper);
                }
            }

            item.Prefix = strPrefix.ToUpper();
            item.Postfix = strPostfix.ToUpper();
            item.Value = value;

            // ȱʡ����Ϊ�����
            if (item.Prefix == "" && item.Postfix == "")
                item.Prefix = "CNY";

            return 0;
        }
示例#8
0
        // ������ɸ��۸��ַ����Ƿ񶼱�ʾ��0?
        // return:
        //      -1  ����
        //      0   ��Ϊ0
        //      1   Ϊ0
        public static int IsZero(List<string> prices,
            out string strError)
        {
            strError = "";

            List<CurrencyItem> items = new List<CurrencyItem>();

            // �任ΪPriceItem
            for (int i = 0; i < prices.Count; i++)
            {
                string strPrefix = "";
                string strValue = "";
                string strPostfix = "";
                int nRet = ParsePriceUnit(prices[i],
                    out strPrefix,
                    out strValue,
                    out strPostfix,
                    out strError);
                if (nRet == -1)
                    return -1;
                decimal value = 0;
                try
                {
                    value = Convert.ToDecimal(strValue);
                }
                catch
                {
                    strError = "���� '" + strValue + "' ��ʽ����ȷ";
                    return -1;
                }

                CurrencyItem item = new CurrencyItem();
                item.Prefix = strPrefix;
                item.Postfix = strPostfix;
                item.Value = value;

                items.Add(item);
            }

            // ����
            for (int i = 0; i < items.Count; i++)
            {
                CurrencyItem item = items[i];

                if (item.Value != 0)
                    return 0;   // �м�����˲�Ϊ0��
            }

            return 1;   // ȫ��Ϊ0
        }