Hi I am trying to write an app for price calculations what i need to do is make cut off points for different dollar values the values are when a product is over $22.00 the deduction $3.30. When a product is over $16.50 and under $22.00 the deduction is dollar value -15%-$1.81 and when the product is under $16.50 the deduction is dollar value -24%-.32cents.I am new to code writing and need help thankyou barry t

Dani AI

Generated

A short, practical correction and a safe Python implementation that follows the thread's three-tier deduction rules (as clarified by and implemented in C# by ). The main fixes: use clear conditional branching for the price bands, treat money with Decimal to avoid floating-point rounding errors, validate/sanitize inputs, and decide whether any user-entered "Deductions" or "Rate" should be applied on top of the computed deduction.

from decimal import Decimal, ROUND_HALF_UP, InvalidOperation

def parse_money(value):
    if isinstance(value, Decimal):
        return value
    s = str(value).replace('$','').replace(',','').strip()
    return Decimal(s)

def apply_tier_deduction(price):
    price = parse_money(price)
    if price >= Decimal('22.00'):
        net = price - Decimal('3.30')
    elif price >= Decimal('16.50'):
        net = price * Decimal('0.85') - Decimal('1.81')
    else:
        net = price * Decimal('0.76') - Decimal('0.32')
    return net.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)

def compute_net_price(price, extra_deductions=Decimal('0.00'), clamp_zero=False):
    p = parse_money(price)
    extra = parse_money(extra_deductions)
    result = apply_tier_deduction(p) - extra
    if clamp_zero and result < Decimal('0.00'):
        result = Decimal('0.00')
    return result.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)

# quick examples:
# compute_net_price('25.00') -> Decimal('21.70')
# compute_net_price('18.00', '0.50') -> Decimal('12.99')

Notes and troubleshooting

  • The conditional approach from is correct; a later C# attempt in the thread accidentally used operators/assignments that produced wrong results.
  • Use Decimal for currency, not float. Quantize once at display time with ROUND_HALF_UP to match typical financial rounding.
  • Validate inputs and test the exact boundary values 16.50 and 22.00. Decide whether negative net prices should be clamped to zero or flagged as errors.
  • If a dynamic percentage "Rate" is needed later, apply it explicitly (e.g., price * (1 - rate)), rather than leaving a parsed variable unused.

Recommended Answers

All 6 Replies

Hi I am trying to write an app for price calculations what i need to do is make cut off points for different dollar values the values are when a product is over $22.00 the deduction $3.30. When a product is over $16.50 and under $22.00 the deduction is dollar value -15%-$1.81 and when the product is under $16.50 the deduction is dollar value -24%-.32cents.I am new to code writing and need help thankyou barry t


$16.50 the deduction is dollar value -24%-.32cents.I am new to code writing and need help thankyou barry t

So wait is that saying when the price in under/over the value you subtract 24% the subract .32 cents for that #?

-T

BTW welcome to daniweb

yes when the dollar value is under $16.50 the deduction is - 24% - 32cents = net value

I'm assuming you meant 22.00 OR Great and 16.50 OR Greater

double valueAfterDeduction(double oldValue){
if(oldValue >= 22.00) oldValue -= 3.3;
else if(oldValue >= 16.5) oldValue =  (oldValue * .85) - 1.81;
else oldValue = (oldValue * .76) - .32;
return oldValue;
}

thankyou campkev I will try your code thankyou for your help barry t

double Price, Rate, Amount, Deductions, NetPrice;

Price = double.Parse(txtPrice.Text);
Rate = double.Parse(txtRate.Text);
Deductions = double.Parse(txtDeductions.Text);

Amount = (Price >= 22.00 - 3.30);
if(Price >= 16.5)Price = (Price * .85)-1.81;
else Price = (Price * .76)-.32;
NetPrice = Price - Amount - Deductions;

txtAmount.Text = Amount.ToString("C");
txtNetPrice.Text = NetPrice.ToString("C");

ok, now I am completely confused. What are you trying to do? What is the Amount variable supposed to represent? What is the rate? You read that in but never use it. And is the user supposed to set his own deductions? I thought that was set.

Anyway, based on your original question and this code i would do the following, but, to be honest, I'm not really sure what you want.

double Price, Rate, Amount, Deductions, NetPrice;

Price = double.Parse(txtPrice.Text);
Rate = double.Parse(txtRate.Text);
Deductions = double.Parse(txtDeductions.Text);

if(Price >= 22.00) Price = Price - 3.30);
else if(Price >= 16.5)Price = (Price * .85)-1.81;
else Price = (Price * .76)-.32;


//txtAmount.Text = Amount.ToString("C"); commented this line out because I don't know what you want here.
txtNetPrice.Text = Price.ToString("C");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.