如何根据数量获得价格? [英] how to get price based on quantity?
本文介绍了如何根据数量获得价格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要根据数量显示价格值...示例我有
string str =28.00 3.00 71.40 6.00 134.40 - 格式正确
这意味着对于1和2的数量,价格应该是28.00,3-5的数量价格是71.00,6及以上价格是134.40
有人可以帮我提供代码。使用正则表达式或SPLIT方法或者它符合我要求的任何方式。
谢谢。
Hi,
I need to display price value based on quantity...Example I have
string str = " 28.00 3.00 71.40 6.00 134.40 " - is in STRING FORMAT
which means for 1 & 2 QUANTITY the PRICE should be 28.00 for 3-5 QUANTITY PRICE is 71.00, for 6 & above PRICE is 134.40
Can someone please help me with the code.using Regular Expressions or SPLIT method or whatever way it meets my requirement.
Thank you.
推荐答案
int qty=5;//YOUR VALUE
stirng[] data=str.Split(' ');
if(data.length>0)
{
decimal price=data[0];
for(int i=1;i<data.length;i+2)
{
if(qty<=data[i])
{
price=convert.toDecimal(data[i-1])*qty;
}
else
{
break;
}
}
}
i dint测试,希望它会工作
i dint tested,hope it will work
(对不起,我的C#
有点生锈)
(Sorry, myC#
is a bit rusty)
public class Pricer
{
Dictionary<double, double> price;
public Pricer(string s)
{
string[] a = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
price = new Dictionary<double, double>();
CultureInfo INVC = CultureInfo.InvariantCulture;
for (int n = 0; n < a.Length - 1; n += 2)
price.Add(double.Parse(a[n+1], INVC), double.Parse(a[n],INVC));
price.Add(double.PositiveInfinity, double.Parse(a[a.Length - 1], INVC));
}
public double getPrice(double quantity)
{
double result = 0.0;
foreach (double d in price.Keys)
if (quantity < d)
{
result = price[d]*quantity;
break;
}
return result;
}
}
然后
then
static void Main(string[] args)
{
string str = " 28.00 3.00 71.40 6.00 134.40 ";
Pricer pr = new Pricer(str);
Console.WriteLine("Quantity {0} price {1}", 0.5, pr.getPrice(0.5));
Console.WriteLine("Quantity {0} price {1}", 5.5, pr.getPrice(5.5));
Console.WriteLine("Quantity {0} price {1}", 1000, pr.getPrice(1000));
}
试试这个
string str=" 28.00 3.00 71.40 6.00 134.40 ";
string[] s=str.split(' '); double price;double unitprice;
if(Quantity==1 || Quatity==2)
{
if(Quantity==2)
price=convert.todouble(s[0]*2);
else
price=s[0];
}
elseif(Quantity==3 || Quatity==4 || Quantity==5)
{
unitprice=convert.todouble(s[2]/3);
if(Quantity==4)
price=unitprice*4;
elseif(Quantity==5)
price=unitprice*5;
else
price=convert.todouble(s[2]);
}
else
{
unitprice=convert.todouble(s[4]/6);
if(Quantity>6)
price=unitprice*Quantity;
else
{
price=convert.todouble(s[4]);
}
这篇关于如何根据数量获得价格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文