Esempio n. 1
0
        static void Inc(Table table,
                        string strEntry,
                        int nColumn,
                        string strPrice)
        {
            Line   line        = table.EnsureLine(strEntry);
            string strOldValue = (string)line[nColumn];

            if (string.IsNullOrEmpty(strOldValue) == true)
            {
                line.SetValue(nColumn, strPrice);
                return;
            }

            // 连接两个价格字符串
            string strPrices = PriceUtil.JoinPriceString(strOldValue,
                                                         strPrice);

            string        strError = "";
            List <string> prices   = null;
            // 将形如"-123.4+10.55-20.3"的价格字符串切割为单个的价格字符串,并各自带上正负号
            // return:
            //      -1  error
            //      0   succeed
            int nRet = PriceUtil.SplitPrices(strPrices,
                                             out prices,
                                             out strError);

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

            string strResult = "";

            nRet = PriceUtil.TotalPrice(prices,
                                        out strResult,
                                        out strError);
            if (nRet == -1)
            {
                throw new Exception(strError);
            }

            line.SetValue(nColumn, strResult);
        }
Esempio n. 2
0
        // 将形如"-CNY123.4+USD10.55-20.3"的价格字符串计算汇率
        // parameters:
        public static string RatePrices(
            List <RateItem> rate_table,
            string strPrices)
        {
            string strError = "";

            strPrices = strPrices.Trim();

            if (String.IsNullOrEmpty(strPrices) == true)
            {
                return("");
            }

            List <string> prices = null;
            // 将形如"-123.4+10.55-20.3"的价格字符串切割为单个的价格字符串,并各自带上正负号
            // return:
            //      -1  error
            //      0   succeed
            int nRet = PriceUtil.SplitPrices(strPrices,
                                             out prices,
                                             out strError);

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

            List <string> changed_prices = new List <string>();

            foreach (string price in prices)
            {
                CurrencyItem item = CurrencyItem.Parse(price);

                RateItem rate = FindBySource(rate_table, item.Prefix, item.Postfix);
                if (rate == null)
                {
                    changed_prices.Add(price);
                    continue;
                }

                CurrencyItem result = rate.Exchange(item);
                changed_prices.Add(result.ToString());
            }

            List <string> results = new List <string>();

            // 汇总价格
            // 货币单位不同的,互相独立
            // return:
            //      -1  error
            //      0   succeed
            nRet = PriceUtil.TotalPrice(changed_prices,
                                        out results,
                                        out strError);
            if (nRet == -1)
            {
                throw new Exception(strError);
            }

#if NO
            StringBuilder text = new StringBuilder();
            for (int i = 0; i < results.Count; i++)
            {
                string strOnePrice = results[i];
                if (String.IsNullOrEmpty(strOnePrice) == true)
                {
                    continue;
                }
                if (strOnePrice[0] == '+')
                {
                    text.Append("+" + strOnePrice.Substring(1));
                }
                else if (strOnePrice[0] == '-')
                {
                    text.Append("-" + strOnePrice.Substring(1));
                }
                else
                {
                    text.Append("+" + strOnePrice);    // 缺省为正数
                }
            }

            return(text.ToString().TrimStart(new char[] { '+' }));
#endif
            return(PriceUtil.JoinPriceString(results));
        }