I am so stuck, I lost 2 days trying to figure this out and here I am! I have an online cart selling subscriptions and I want to offer customers the ability to subscribe and choose their own payment schedule by paying monthly, quarterly or annually. Further, I want to offer a 10% discount to quarterly subscribers and 20% to annual subscribers.

I use 3 methods to do my math and for the purpose of this post, I will make the possible input parameters static.

protected decimal getTotal()
{
	decimal a = Math.Round(Convert.ToDecimal(hf_hostingpackage.Value), 2);
	decimal b = Math.Round(Convert.ToDecimal(hf_savingspct.Value), 2);
	decimal c = Math.Round(Convert.ToDecimal(hf_registerdomain.Value), 2);
	decimal d = Math.Round(Convert.ToDecimal(hf_transferdomain.Value), 2);
	decimal m_totals = (a - (a * b) + c + d); ;
	m_totals = Math.Floor(m_totals * 100) / 100;
	return m_totals;
}

protected decimal getSavings()
{
	decimal a = Math.Round(Convert.ToDecimal(hf_hostingpackage.Value), 2);
	decimal b = Math.Round(Convert.ToDecimal(hf_savingspct.Value), 2);
	decimal m_savings = (a - (a * b));
	m_savings = Math.Floor(m_savings * 100) / 100;
	return m_savings;
}

protected decimal getCycleCost()
{
	decimal a = Math.Round(Convert.ToDecimal(hf_hostingpackage.Value), 2);
	decimal b = Math.Round(Convert.ToDecimal(hf_savingspct.Value), 2);
	decimal c = Convert.ToInt32(hf_billingcycle.Value);
	decimal m_cyclecost = (a - (a * b)) * c;
	m_cyclecost = Math.Floor(m_cyclecost * 100) / 100;
	return m_cyclecost;
}

Using option 1 parameters I get $17.99 x 3 = $53.97 per Quarter.
This is CORRECT!

hf_hostingpackage.Value = 19.99
hf_savingspct.Value = 0.10
hf_registerdomain.Value = 0.00
hf_transferdomain.Value = 0.00
hf_billingcycle.Value = 3

Using option 2 parameters I get $15.99 x 12 = $191.90 per Year.
This is WRONG! 15.99 x 12 equals 191.88 with my public school education :)

hf_hostingpackage.Value = 19.99
hf_savingspct.Value = 0.20
hf_registerdomain.Value = 0.00
hf_transferdomain.Value = 0.00
hf_billingcycle.Value = 12

So I'm throwing it out to you pro's and I a very thankful for any advice!

Recommended Answers

All 2 Replies

(a - (a * b)) , where a = 19.99 and b = 0.2, equals 15.992. It you multiply it by 12, you get 191.904, which rounds to 191.90 on line 27. You would have to round the calculation (a - (a * b)) before multiplying it by c to achieve your results.

Thanks nmaillet, you were mostly correct but the answer I was looking for was in what type of rounding needed to be done to get the desired result. Since C#'s math library doesn't offer a "round down to nearest tenth" method, I found the answer in MidpointRounding. I replaced line 26 with the code snippet below and removed line 27 completely.

decimal m_cyclecost = Math.Round((a - (a * b)), 2, MidpointRounding.AwayFromZero) * 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.