我想要一种算法来计算
比例表:
<$ 10,000 => 13%
$ 10,001-$ 100,000 => 8%
$ 100,001-$ 1,000,000 => 6%
$ 1,000,001-$ 5,000,000 => 4.9%
5,000,000美元=> 3%
如果用户输入$ 6,000,000
预期输出
10,000美元* 13%= 1,300美元
$ 90,000 * 8%= $ 7,200
900,000美元* 6%= 54,000美元
$ 4,000,000 * 4.9%= $ 196,000
2,000,000美元* 3%= 60,000美元
------------------------总计:$ 318,000
如果用户输入$ 5,000
预期输出
10,000美元= 5,000美元+ 13%= 650美元
$ 90,000 = 0 + 8%= $ 0
900,000美元= 0 + 6%= 0美元
$ 4,000,000 = 0 + 4.9%= $ 0
$ 2,000,000 = 0 + 3%= $ 0
---------------------------总计= $ 650
这是我到目前为止的代码:
def processButton(self):
name_list = []
name = self.name.get()
name_list.append(name)
print('Names list consists of: ', name_list)
return name_list
答案 0 :(得分:1)
尝试一下
public class OverheadAmountScale
{
public string ScaleTitle { get; set; }
public decimal Value { get; set; }
public decimal Rate { get; set; }
public decimal OverHeadAmount
{
get
{
return decimal.Round(Value * Rate, 2, MidpointRounding.AwayFromZero);
}
}
}
static List<OverheadAmountScale> GetScales(decimal amount)
{
var amountScales = new List<OverheadAmountScale>();
const decimal TOP_THRESHOLD = 5000000m;
const decimal HIGH_THRESHOLD = 1000000m;
const decimal MEDIUM_THRESHOLD = 100000m;
const decimal LOW_THRESHOLD = 10000m;
const decimal BASE_THRESHOLD = 0m;
const decimal TOP_THRESHOLD_FACTOR = .03m;
const decimal HIGH_THRESHOLD_FACTOR = .049m;
const decimal MEDIUM_THRESHOLD_FACTOR = .06m;
const decimal LOW_THRESHOLD_FACTOR = .08m;
const decimal BASE_THRESHOLD_FACTOR = .13m;
decimal amountWorkingRange = amount;
decimal OverheadDue = 0;
if (amountWorkingRange > TOP_THRESHOLD)
{
var lineOverheadTOP = amountWorkingRange - TOP_THRESHOLD;
amountScales.Add(
new OverheadAmountScale
{
ScaleTitle = $">= ${(TOP_THRESHOLD + 1).RoundResultNoDecimal().ToString("N0")}",
Value = lineOverheadTOP,
Rate = TOP_THRESHOLD_FACTOR,
});
amountWorkingRange = TOP_THRESHOLD;
OverheadDue += lineOverheadTOP;
}
if (amountWorkingRange > HIGH_THRESHOLD)
{
var lineOverheadHIGH = amountWorkingRange - HIGH_THRESHOLD;
amountScales.Add(
new OverheadAmountScale
{
ScaleTitle = $"${(HIGH_THRESHOLD.RoundResultNoDecimal() + 1).ToString("N0")} - ${TOP_THRESHOLD.RoundResultNoDecimal().ToString("N0")}",
Value = lineOverheadHIGH,
Rate = HIGH_THRESHOLD_FACTOR,
});
OverheadDue += lineOverheadHIGH;
amountWorkingRange = HIGH_THRESHOLD;
}
if (amountWorkingRange > MEDIUM_THRESHOLD)
{
var lineOverheadMEDIUM = amountWorkingRange - MEDIUM_THRESHOLD;
amountScales.Add(
new OverheadAmountScale
{
ScaleTitle = $"${(MEDIUM_THRESHOLD + 1).RoundResultNoDecimal().ToString("N0")} - ${HIGH_THRESHOLD.RoundResultNoDecimal().ToString("N0")}",
Value = lineOverheadMEDIUM,
Rate = MEDIUM_THRESHOLD_FACTOR,
});
OverheadDue += lineOverheadMEDIUM;
amountWorkingRange = MEDIUM_THRESHOLD;
}
if (amountWorkingRange > LOW_THRESHOLD)
{
var lineOverheadLOW = amountWorkingRange - LOW_THRESHOLD;
amountScales.Add(
new OverheadAmountScale
{
ScaleTitle = $"{(LOW_THRESHOLD + 1).RoundResultNoDecimal().ToString("N0")} - ${MEDIUM_THRESHOLD.RoundResultNoDecimal().ToString("N0")}",
Value = lineOverheadLOW,
Rate = LOW_THRESHOLD_FACTOR,
});
OverheadDue += lineOverheadLOW;
amountWorkingRange = LOW_THRESHOLD;
}
if (amountWorkingRange > BASE_THRESHOLD)
{
var lineOverheadBASE = amountWorkingRange - BASE_THRESHOLD;
amountScales.Add(
new OverheadAmountScale
{
ScaleTitle = $"<= ${LOW_THRESHOLD.RoundResultNoDecimal().ToString("N0")}",
Value = lineOverheadBASE,
Rate = BASE_THRESHOLD_FACTOR,
});
OverheadDue += lineOverheadBASE;
amountWorkingRange = BASE_THRESHOLD;
}
return amountScales;
}
}