Divide Into Monthly Installments - c#

Hi I'm trying to make Monthly Installments of a "double value"
The Problem is that the decimal values get divided too, and i don't need that happen.
Example :
List<Installments> InstallmentList {get; set;}
for (int i = 0 ; int i <= Month ; i++)
{
double Value = 90.10 ;
int Month = 3;
InstallmentCost = Value / Month;
InstallmentList.Add (new Installment {InstallmentCost = example.InstallmentCost} )
}
Doing That i will get a list of Installments where the value will be :
Installment = 30.03333333333333;
Installment = 30.03333333333333;
Installment = 30.03333333333333;
But I need that the decimals do not divide and and only the last Installment gets it
Example Of The Results that i need :
Installment = 30.00;
Installment = 30.00;
Installment = 30.10;

Just truncate the installment which only takes the integral part (if not C# then convert to something like int and back to double would do the trick!).
I have used C#, here's the working solution:-
double Value = 90.10;
int Month = 3;
for (int i = 1; i <= Month ; i++)
{
var installmentCost = Math.Truncate(Value / Month);
InstallmentList.Add(new Installment {InstallmentCost = installmentCost});
}
// Extract pending balance to be adjusted, total - the sum of all installments
double pendingBalanceToAdjust = Value - InstallmentList.Sum((s) => s.InstallmentCost);
// Update to the last installment
if (pendingBalanceToAdjust > 0)
InstallmentList.Last().InstallmentCost += pendingBalanceToAdjust;

You can calculate the remainder at the start and then divide the rest into equal parts:
double value = 90.10;
int month = 3;
// calculate the remainder with precision 0.1
double remainder = value % (month * 0.1);
double installmentValue = (value - remainder) / month;
for (int i = 0; i < month - 1; i++)
InstallmentList.Add(new Installment {InstallmentCost = installmentCost});
InstallmentList.Add(new Installment {InstallmentCost = installmentCost + remainder});
the expression value % (month * 0.1) effectively works out what is left over if you keep giving each of the 3 months 0.1 from the value until you can no longer carry on.
Changing the precision to 0.01 will change the outcome to: 30.03, 30.03, 30.04

Linq approach
decimal value = 90.10m;
int month = 3;
List<Installment> installments = Enumerable.Range(0, month).Select(x => new Installment() { InstallmentCost = Math.Floor(value / month) }).ToList();
installments.Last().InstallmentCost += (value - installments.Sum(x => x.InstallmentCost));

Related

Converting a ASCII value into its corresponding Integer value

The objective of this code is to add all the integers in a whole number into one value (e.g "2013" => 6),
In c# I have written the code so it outputs the number to its corresponding ASCII value one at a time, but I am at a loss at how to convert it back into its number value.
Note that I am new at C#
string Year;
int Total = 0;
int Adding = 0;
int Adding2 = 0;
Console.WriteLine("Write The year you want converted");
Year = Console.ReadLine();
for (int i = 0; i < Year.Length; i++)
{
Adding2 = Year[i];
Adding = Convert.ToInt32(Adding2);
Total = Adding + Total;
Console.WriteLine(Total);
}
You should sum values, not ascii codes:
...
for (int i = 0; i < Year.Length; i++)
{
Adding2 = Year[i];
Adding = Adding2 - '0';
Total = Adding + Total;
}
Console.WriteLine(Total);
In general case, you can use char.GetNumericValue():
// double: some characters have fractional values: '⅝'
double Total = 0.0;
foreach (char c in Year) {
double value = char.GetNumericValue(c);
// If character has value (e.g. 'A' doesn't have)
if (value != -1)
Total += value;
}
Console.WriteLine(Total);

How to divide a decimal number into rounded parts that add up to the original number?

All Decimal numbers are rounded to 2 digits when saved into application. I'm given a number totalAmount and asked to divide it into n equal parts(or close to equal).
Example :
Given : totalAmount = 421.9720; count = 2 (totalAmount saved into application is 421.97)
Expected : 210.99, 210.98 => sum = 421.97
Actual(with plain divide) : 210.9860 (210.99), 210.9860 (210.99) => sum = 412.98
My approach :
var totalAmount = 421.972m;
var count = 2;
var individualCharge = Math.Floor(totalAmount / count);
var leftOverAmount = totalAmount - (individualCharge * count);
for(var i = 0;i < count; i++) {
Console.WriteLine(individualCharge + leftOverAmount);
leftOverAmount = 0;
}
This gives (-211.97, -210)
public IEnumerable<decimal> GetDividedAmounts(decimal amount, int count)
{
var pennies = (int)(amount * 100) % count;
var baseAmount = Math.Floor((amount / count) * 100) / 100;
foreach (var _ in Enumerable.Range(1, count))
{
var offset = pennies-- > 0 ? 0.01m : 0m;
yield return baseAmount + offset;
}
}
Feel free to alter this if you want to get an array or an IEnumerable which is not deferred. I updated it to get the baseAmount to be the floor value so it isn't recalculated within the loop.
Basically you need to find the base amount and a total of all the leftover pennies. Then, simply add the pennies back one by one until you run out. Because the pennies are based on the modulus operator, they'll always be in the range of [0, count - 1], so you'll never have a final leftover penny.
You're introducing a few rounding errors here, then compounding them. This is a common problem with financial data, especially when you have to constrain your algorithm to only produce outputs with 2 decimal places. It's worse when dealing with actual money in countries where 1 cent/penny/whatever coins are no longer legal tender. At least when working with electronic money the rounding isn't as big an issue.
The naive approach of dividing the total by the count and rounding the results is, as you've already discovered, not going to work. What you need is some way to spread out the errors while varying the output amounts by no more than $0.01. No output value can be more than $0.01 from any other output value, and the total must be the truncated total value.
What you need is a way to distribute the error across the output values, with the smallest possible variation between the values in the result. The trick is to track your error and adjust the output down once the error is high enough. (This is basically how the Bresenham line-drawing algorithm figures out when to increase the y value, if that helps.)
Here's the generalized form, which is pretty quick:
public IEnumerable<decimal> RoundedDivide(decimal amount, int count)
{
int totalCents = (int)Math.Floor(100 * amount);
// work out the true division, integer portion and error values
float div = totalCents / (float)count;
int portion = (int)Math.Floor(div);
float stepError = div - portion;
float error = 0;
for (int i = 0; i < count; i++)
{
int value = portion;
// add in the step error and see if we need to add 1 to the output
error += stepError;
if (error > 0.5)
{
value++;
error -= 1;
}
// convert back to dollars and cents for outputput
yield return value / 100M;
}
}
I've tested it with count values from 1 through 100, all outputs sum to match the (floored) input value exactly.
Try to break it down to steps:
int decimals = 2;
int factor = (int)Math.Pow(10, decimals);
int count = 2;
decimal totalAmount = 421.97232m;
totalAmount = Math.Floor(totalAmount * factor) / factor; // 421.97, you may want round here, depends on your requirement.
int baseAmount = (int)(totalAmount * factor / count); // 42197 / 2 = 21098
int left = (int)(totalAmount * factor) % count; // 1
// Adding back the left for Mod operation
for (int i = 0; i < left; i++)
{
Console.WriteLine((decimal)(baseAmount + 1) / factor); // 21098 + 1 / 100 = 210.99
}
// The reset that does not needs adjust
for (int i = 0; i < count - left; i++)
{
Console.WriteLine((decimal)baseAmount / factor); // 21098 / 100 = 210.98
}

Overcoming a discrepancy in a division

I have the following code to divide an amount by a number and allocate the result as an amount that needs to be paid per month.
objData.month_per_amount = (Convert.ToDecimal(txtAmount.Value) / Convert.ToInt32(txtMonths.Value));
In a scenario example if I divide 13 by 3 and round off the result to 2 decimal places I get 4.33 for each month. But when I multiply 4.33 by 3 I am getting 12.99, which is not equivalent to 13. There is a discrepancy of 0.01. In this scenario how can I allocate like below:
month 1: 4.33
month 2: 4.33
month 3: 4.34
Hope I made it clear, the preferred code should only be executed if there is such a discrepancy, for example if 14 is to be divided by 2, we get 7 for each month and 7+7=14, so exactly the same figure we are getting here.
In accounting you'd often use something called 'reducing balance' for this. The idea is that you calculate the month's total, deduct it from the overall total and reduce the number of months. So something like:
decimal balance = 13m;
int months = 3;
int monthsRemaining = 3;
for (var i = 0; i < months; i++)
{
decimal thisMonth = Math.Round(balance / monthsRemaining, 2);
balance -= thisMonth;
monthsRemaining -= 1;
Console.WriteLine("Month {0}: {1}", i + 1, thisMonth);
}
This will result in 4.33, 4.34, 4.33.
The benefit of this method is that the rounding errors are distributed fairly evenly throughout the period rather than all in one month. For example, 100 over 24 months using that method would result in 23 payments of 4.17 and 1 of 4.09 whereas reducing balance would be 4.16 or 4.17 each month.
You do not have to check the remainder. A more efficient C# code (in terms of the required computation) would be like the following.
double amount = 13;
int months = 3;
int precision = 2;
double[] amountForEachMonth = new double[months];
double temp = Math.Round(amount / months, precision);
for (int i = 0 ; i < months - 1 ; i++)
amountForEachMonth[i] = temp;
amountForEachMonth[months - 1] = amount - (temp * (months - 1)) ;
You don't need to make it a special case when there is a discrepancy, you can simply always calculate the payment of the last month as what's left to pay to reach the total amount. If there is no discrepancy then it will be the same value anyway. Example:
int months = Convert.ToInt32(txtMonths.Value);
decimal amount = Convert.ToDecimal(txtAmount.Value);
month_per_amount = Decimal.Round(amount / months, 2);
decimal last_month = amount - (months - 1) * month_per_amount;
for (int month = 1; month <= months; month++) {
decimal monthly = month < months ? month_per_amount : last_month;
Console.WriteLine("Month {0}: {1}", month, monthly);
}
if " amount % month == 0 " , no discrepancy occures. otherwise, the last item should be a little more than others .
(The code here may have some syntax issues, I wanted to show you the algorithm.)
decimal amount = Convert.ToDecimal(txtAmount.Value);
int month = Convert.ToInt32(txtMonths.Value);
int n = 3;
decimal amounts[3];//n = 3
for (int i = 0 ; i < n-1 ; i++)
amounts[i] = amount / month;
if ( amount % month != 0 ) {
amounts[n-1] = amount - ( amount / month * (n-1) ) ;
else
amounts[n-1] = amount / month ;

char array to string specifying an index

I am trying make a clock. The hour is a string. I want to put that hour into a char array so i can separate the hour into one or two indexes. That way i can use a case on the individual indexes to ultimately bind it to a grid and draw a line for the digital time..
So, the hour is converted to an array. But i want to take the first index 0 and store it into a string or int so i can pass it into a function where i can use a case on it. if i leave it as a char and convert it to an int i get a number like 50 which is no good.
So, when i try to assign the first index of the array to a string it wont let me convert from array to string.
hr1 = hours[0];
What is my best option of seperating the hour into separate indexes and then converting it over to the proper int? Also, the time is on 24 hour and i would like it to be 12 hour.
private void _timer_Elapsed(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
//DigitalTime = now.ToString("hh:mm:ss tt");
//DigitalTime = now.ToString();
//DigitalTime = DateTime.Now.Hour.ToString();
SecondAngle = now.Second * 6;
MinuteAngle = now.Minute * 6;
HourAngle = (now.Hour * 30) + (now.Minute * 0.5);
string hrs, hr1, hr2;
char[] hours = new char[15];
hrs = DateTime.Now.Hour.ToString("hh:mm:ss tt");
hours = hrs.ToCharArray();
if (hours.Length > 1)
{
hr1 = hours[0]; // error -
hr2 = hours[1];
// SetHourDigit1(Convert.ToInt32(hr1));
}
else
{
// hr1 = '0';
hr2 = hours[0];
}
}
public void SetHourDigit1(int num)
{
switch (num)
{
case 0:
MessageBox.Show("num" + num);
break;
case 1:
MessageBox.Show("num" + num);
break;
case 2:
break;
}
}
I would avoid messing with strings and char arrays altogether. Use arithmetic instead:
int hour = DateTime.Now.Hour;
int leastSignificantDigit = hour % 10;
int mostSignificantDigit = hour / 10;
// Use one of these as input for your switch statement.
% is the modulo operator; the remainder of a division by 10 in this case.
Edit: I noticed you want to have a 12-hour clock. You can add some additional computation for this. Replacement for the first line of code:
int hour = DateTime.Now.Hour % 12;
if (hour == 0) hour = 12;
if (hours.Length > 1)
{
hr1 = hours[0].ToString(); // no error -
hr2 = hours[1].ToString();
// SetHourDigit1(Convert.ToInt32(hr1));
}
but if you want to get parts of time use this:
dateparts = datestring.splite(':');
string hour = dateparts[0];
string minute = dateparts[1];
string s = dateparts[2];
now you have hour,minute,second and t.
because of you can trust the parts use int.parse to convert them to int.
int nhour = int.parse(hour);
int nminute = int.parse(minute);
int nsecond = int.parse(s);
for 24 hours
hrs = DateTime.Now.Hour.ToString("HH:mm:ss");
This is a usefull link for u:
DateTime.ToString() Pattern
Use the modulo (%) operator to convert the 24 hour value to 12 hours, and also to get the second digit of the two digit number. There is no reason to format it as a string and then convert it back to numbers.
private void _timer_Elapsed(object sender, EventArgs e) {
DateTime now = DateTime.Now;
int hour12 = now.Hour % 12;
SecondAngle = now.Second * 6;
MinuteAngle = now.Minute * 6;
HourAngle = (hour12 * 30) + (now.Minute * 0.5);
SetHourDigit1(hour12 / 10);
SetHourDigit2(hour12 % 10);
}

spliting 6 digit int into 3 parts?

I want to get a six digit number by user and spit it into 3 parts as(day, month, year)
Example:
int date=111213;
day =11;
month =12;
year =13;
I think I have to convert it into string then by using substring() I can do this.
Any easy Idea ??
How about:
// Assuming a more sensible format, where the logically most significant part
// is the most significant part of the number too. That would allow sorting by
// integer value to be equivalent to sorting chronologically.
int day = date % 100;
int month = (date / 100) % 100;
int year = date / 10000;
// Assuming the format from the question (not sensible IMO)
int year = date % 100;
int month = (date / 100) % 100;
int day = date / 10000;
(Do you have to store your data like this to start with? Ick.)
Storing a date as an integer like this isn't ideal, but if you must do it -- and you're sure that the number will always use the specified format -- then you can easily extract the day, month and year:
int day = date / 10000;
int month = (date / 100) % 100;
int year = date % 100;
You can do this with modular arithmetic:
int day = date / 10000;
int month = (date / 100) % 100;
int year = date % 100;
Here is the solution in Java with no optimization:
final int value = 111213;
int day;
int month;
int year;
day = value / 10000;
month = (value - (day * 10000)) / 100;
year = (value - (day * 10000)) - month * 100;

Categories

Resources