Good implementation of a Change Calculator in C# - c#

Given an amount in ml and 3 pack sizes (20ml, 200ml and 1000ml) I'd like to calculate how many of each packs are needed to fulfill the total amount.
E.g.
Amount = 3210ml
1000ml = 3 packs
200ml = 1 pack
20ml = 1 pack (always round up to nearest quantity)
This is pretty much just like a change calculator, and I'm looking for the right way to do it.
Here's my attempt
public class PackSizeCalculator
{
private const int LargePackSize = 1000;
private const int MediumPackSize = 200;
private const int SmallPackSize = 20;
public int LargePacks {get; set;}
public int MediumPacks {get; set;}
public int SmallPacks {get; set;}
public PackSizeCalculator(int amount)
{
int remainder = amount;
while(remainder > 0) {
if(remainder >= LargePackSize)
{
LargePacks = remainder / LargePackSize;
remainder = remainder % LargePackSize;
}
else if(remainder >= MediumPackSize)
{
MediumPacks = remainder / MediumPackSize;
remainder = remainder % MediumPackSize;
}
else if(remainder > SmallPackSize)
{
if(remainder % SmallPackSize == 0)
{
SmallPacks = (remainder / SmallPackSize);
}
else {
SmallPacks = (remainder / SmallPackSize) + 1;
}
remainder = 0;
}
else {
SmallPacks = 1;
remainder = 0;
}
}
}
}
Is this a good way to go about it or would you recommend something different?

Something like this:
const int LARGE_PACK = 1000;
const int MEDIUM_PACK = 200;
const int SMALL_PACK = 20;
int largePacks = (int)(Amount / LARGE_PACK);
int mediumPacks = (int)((Amount % LARGE_PACK) / MEDIUM_PACK);
int smallPacks = (int)ceil(((float)((Amount % LARGE_PACK) % MEDIUM_PACK) / SMALL_PACK));

You can create a function like this:
struct Change
{
public int Large;
public int Medium;
public int Small;
}
private Change CalculateChange(int TotalAmount)
{
Change result;
result.Large = TotalAmount >= 1000 ? TotalAmount / 1000 : 0;
result.Medium = TotalAmount % 1000 >= 200 ? (TotalAmount % 1000) / 200 : 0;
result.Small = (TotalAmount % 1000) % 200 >= 20 ? ((TotalAmount % 1000) % 200) / 20 : 0;
return result;
}
At the end you will have your result of Change struct Type.

Related

C# Winforms: Returning values to a button event

I'm completely new to C# programming, and I'm trying to make a custom calculator using Windows Forms.
Three text boxes txtMinSkill, txtMaxSkill, txtCooldown should be keyed values into, and clicking a button buttonCalculate should do some calculations and present the result in a label resultLabel.
I have managed to get everything working down to the skill.Display function, but I have no idea how to display the variables hours, minutes, seconds in the label. When I try to access them from within the button event, I just get a message that it does not exist in the current context. And I can't access the label from the Display method.
Can anyone assist? Thanks!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCalculate_Click(object sender, EventArgs e)
{
double minSkill;
double maxSkill;
double coolDown;
minSkill = float.Parse(txtMinSkill.Text) * 10;
maxSkill = float.Parse(txtMaxSkill.Text) * 10;
coolDown = float.Parse(txtCooldown.Text);
SkillGainCalculator skill = new SkillGainCalculator();
skill.IntegerMax(maxSkill);
skill.RemainderMax(maxSkill);
skill.RemainderMin(minSkill);
skill.IntegerMin(minSkill);
skill.Calculate(minSkill,maxSkill);
skill.Display(coolDown);
}
}
class SkillGainCalculator
{
//member variables
private int integerMax;
private int remainderMax;
private int integerMin;
private int remainderMin;
private double counter;
const int constant = 6480;
private int i;
private double totalTime;
private int hours;
private int minutes;
private int seconds;
public double IntegerMax(double intMax)
{
integerMax = (int)((1000 - intMax) / 50);
return integerMax;
}
public int RemainderMax(double intMax)
{
remainderMax = (int)((1000 - intMax) % 50);
return remainderMax;
}
public int RemainderMin(double intMin)
{
remainderMin = (int)((1000 - intMin) % 50);
return remainderMin;
}
public int IntegerMin(double intMin)
{
if (remainderMin == 0)
{
integerMin = (int)((1000 - intMin) / 50) - 1;
return integerMin;
}
else
{
integerMin = (int)((1000 - intMin) / 50);
return integerMin;
}
}
public double Calculate(double intMax, double intMin)
{
for (i = integerMax; i <= integerMin; i++)
{
if (i == integerMax && remainderMax != 0)
{
if (intMax <= 700)
{
counter = counter + constant * Math.Pow(0.8, i) * (50 - remainderMax) / 50;
}
else
{
counter = counter + constant * Math.Pow(0.8, i) * (50 - remainderMax) / 50;
}
}
else if (i == integerMin && remainderMin != 0)
{
if (intMin < 700)
{
counter = counter + constant * Math.Pow(0.8, i) * remainderMin / 50;
}
else
{
counter = counter + constant * Math.Pow(0.8, i) * remainderMin / 50;
}
}
else if (i >= 6)
{
counter = counter + constant * Math.Pow(0.8, i);
}
else
{
counter = counter + constant * Math.Pow(0.8, i);
}
}
return counter;
}
public void Display(double clD)
{
totalTime = counter * clD / 3600;
hours = (int)(counter * clD / 3600);
minutes = (int)((totalTime - hours) * 3600 / 60);
seconds = (int)((totalTime - hours) * 3600 % 60);
}
}
I have no idea, what your code does and as #Steve already said your question misses some key infos. Nevertheless try changing your two methods Display and buttonCalculate_Click like this:
public string Display(double clD)
{
totalTime = counter * clD / 3600;
hours = (int)(counter * clD / 3600);
minutes = (int)((totalTime - hours) * 3600 / 60);
seconds = (int)((totalTime - hours) * 3600 % 60);
return "Hours: " + hours + ", Minutes: " + minutes + ", Seconds: " + seconds;
}
private void buttonCalculate_Click(object sender, EventArgs e)
{
double minSkill;
double maxSkill;
double coolDown;
minSkill = float.Parse(txtMinSkill.Text) * 10;
maxSkill = float.Parse(txtMaxSkill.Text) * 10;
coolDown = float.Parse(txtCooldown.Text);
SkillGainCalculator skill = new SkillGainCalculator();
skill.IntegerMax(maxSkill);
skill.RemainderMax(maxSkill);
skill.RemainderMin(minSkill);
skill.IntegerMin(minSkill);
skill.Calculate(minSkill, maxSkill);
resultLabel.Text = skill.Display(coolDown);
}
The method Display now generates the string you want to display and returns it when called so you can set resultLabel.Text from you calling method:

RSI vs Wilder's RSI Calculation Problems

I am having trouble getting a smoothed RSI. The below picture is from freestockcharts.com. The calculation uses this code.
public static double CalculateRsi(IEnumerable<double> closePrices)
{
var prices = closePrices as double[] ?? closePrices.ToArray();
double sumGain = 0;
double sumLoss = 0;
for (int i = 1; i < prices.Length; i++)
{
var difference = prices[i] - prices[i - 1];
if (difference >= 0)
{
sumGain += difference;
}
else
{
sumLoss -= difference;
}
}
if (sumGain == 0) return 0;
if (Math.Abs(sumLoss) < Tolerance) return 100;
var relativeStrength = sumGain / sumLoss;
return 100.0 - (100.0 / (1 + relativeStrength));
}
https://stackoverflow.com/questions/...th-index-using-some-programming-language-js-c
This seems to be the pure RSI with no smoothing. How does a smoothed RSI get calculated? I have tried changing it to fit the definitions of the these two sites however the output was not correct. It was barely smoothed.
(I don't have enough rep to post links)
tc2000 -> Indicators -> RSI_and_Wilder_s_RSI (Wilder's smoothing = Previous MA value + (1/n periods * (Close - Previous MA)))
priceactionlab -> wilders-cutlers-and-harris-relative-strength-index (RS = EMA(Gain(n), n)/EMA(Loss(n), n))
Can someone actually do the calculation with some sample data?
Wilder's RSI vs RSI
In order to calculate the RSI, you need a period to calculate it with. As noted on Wikipedia, 14 is used quite often.
So the calculation steps would be as follows:
Period 1 - 13, RSI = 0
Period 14:
AverageGain = TotalGain / PeriodCount;
AverageLoss = TotalLoss / PeriodCount;
RS = AverageGain / AverageLoss;
RSI = 100 - 100 / (1 + RS);
Period 15 - to period (N):
if (Period(N)Change > 0
AverageGain(N) = ((AverageGain(N - 1) * (PeriodCount - 1)) + Period(N)Change) / PeriodCount;
else
AverageGain(N) = (AverageGain(N - 1) * (PeriodCount - 1)) / PeriodCount;
if (this.Change < 0)
AverageLoss(N) = ((AverageLoss(N - 1) * (PeriodCount - 1)) + Math.Abs(Period(N)Change)) / PeriodCount;
else
AverageLoss(N) = (AverageLoss(N - 1) * (PeriodCount - 1)) / PeriodCount;
RS = AverageGain / AverageLoss;
RSI = 100 - (100 / (1 + RS));
Thereafter, to smooth the values, you need to apply a moving average of a certain period to your RSI values. To do that, traverse your RSI values from the last index to the first and calculate your average for the current period based on the preceding x smoothing periods.
Once done, just reverse the list of values to get the expected order:
List<double> SmoothedRSI(IEnumerable<double> rsiValues, int smoothingPeriod)
{
if (rsiValues.Count() <= smoothingPeriod)
throw new Exception("Smoothing period too large or too few RSI values passed.");
List<double> results = new List<double>();
List<double> reversedRSIValues = rsiValues.Reverse().ToList();
for (int i = 1; i < reversedRSIValues.Count() - smoothingPeriod - 1; i++)
results.Add(reversedRSIValues.Subset(i, i + smoothingPeriod).Average());
return results.Reverse().ToList();
}
The Subset method is just a simple extension method as follows:
public static List<double> Subset(this List<double> values, int start, int end)
{
List<double> results = new List<double>();
for (int i = start; i <= end; i++)
results.Add(values[i]);
return results;
}
Disclaimer, I did not test the code, but it should give you an idea of how the smoothing is applied.
You can't get accurate values without buffers / global variables to store data.
This is a smoothed indicator, meaning it doesn't only use 14 bars but ALL THE BARS:
Here's a step by step article with working and verified source codes generating exactly the same values if prices and number of available bars are the same, of course (you only need to load the price data from your source):
Tested and verified:
using System;
using System.Data;
using System.Globalization;
namespace YourNameSpace
{
class PriceEngine
{
public static DataTable data;
public static double[] positiveChanges;
public static double[] negativeChanges;
public static double[] averageGain;
public static double[] averageLoss;
public static double[] rsi;
public static double CalculateDifference(double current_price, double previous_price)
{
return current_price - previous_price;
}
public static double CalculatePositiveChange(double difference)
{
return difference > 0 ? difference : 0;
}
public static double CalculateNegativeChange(double difference)
{
return difference < 0 ? difference * -1 : 0;
}
public static void CalculateRSI(int rsi_period, int price_index = 5)
{
for(int i = 0; i < PriceEngine.data.Rows.Count; i++)
{
double current_difference = 0.0;
if (i > 0)
{
double previous_close = Convert.ToDouble(PriceEngine.data.Rows[i-1].Field<string>(price_index));
double current_close = Convert.ToDouble(PriceEngine.data.Rows[i].Field<string>(price_index));
current_difference = CalculateDifference(current_close, previous_close);
}
PriceEngine.positiveChanges[i] = CalculatePositiveChange(current_difference);
PriceEngine.negativeChanges[i] = CalculateNegativeChange(current_difference);
if(i == Math.Max(1,rsi_period))
{
double gain_sum = 0.0;
double loss_sum = 0.0;
for(int x = Math.Max(1,rsi_period); x > 0; x--)
{
gain_sum += PriceEngine.positiveChanges[x];
loss_sum += PriceEngine.negativeChanges[x];
}
PriceEngine.averageGain[i] = gain_sum / Math.Max(1,rsi_period);
PriceEngine.averageLoss[i] = loss_sum / Math.Max(1,rsi_period);
}else if (i > Math.Max(1,rsi_period))
{
PriceEngine.averageGain[i] = ( PriceEngine.averageGain[i-1]*(rsi_period-1) + PriceEngine.positiveChanges[i]) / Math.Max(1, rsi_period);
PriceEngine.averageLoss[i] = ( PriceEngine.averageLoss[i-1]*(rsi_period-1) + PriceEngine.negativeChanges[i]) / Math.Max(1, rsi_period);
PriceEngine.rsi[i] = PriceEngine.averageLoss[i] == 0 ? 100 : PriceEngine.averageGain[i] == 0 ? 0 : Math.Round(100 - (100 / (1 + PriceEngine.averageGain[i] / PriceEngine.averageLoss[i])), 5);
}
}
}
public static void Launch()
{
PriceEngine.data = new DataTable();
//load {date, time, open, high, low, close} values in PriceEngine.data (6th column (index #5) = close price) here
positiveChanges = new double[PriceEngine.data.Rows.Count];
negativeChanges = new double[PriceEngine.data.Rows.Count];
averageGain = new double[PriceEngine.data.Rows.Count];
averageLoss = new double[PriceEngine.data.Rows.Count];
rsi = new double[PriceEngine.data.Rows.Count];
CalculateRSI(14);
}
}
}
For detailed step-by-step instructions, I wrote a lengthy article, you can check it here: https://turmanauli.medium.com/a-step-by-step-guide-for-calculating-reliable-rsi-values-programmatically-a6a604a06b77
P.S. functions only work for simple indicators (Simple Moving Average), even Exponential Moving Average, Average True Range absolutely require global variables to store previous values.

Adding and subtracting vulgar fractions

I'm trying to add two vulgar fractions together by finding the lowest common denominator and then adding. However, my code isn't behaving as expected, and is outputting two very high negative numbers. When I change the second fraction to 3/15 it outputs 0/0.
Here is my main program code:
class Program
{
static void Main(string[] args)
{
Fraction n = new Fraction(2, 4);
Fraction z = new Fraction(3, 12);
Fraction sum = n.Add(z, n);
int num = sum.Numerator;
int den = sum.Denominator;
Console.WriteLine("{0}/{1}", num, den);
Console.ReadKey(true);
}
}
Here is my Fraction class code:
internal class Fraction
{
public Fraction(int numerator, int denominator)
{
Numerator = numerator;
Denominator = denominator;
}
public int Numerator { get; private set; }
public int Denominator { get; private set; }
public Fraction Add(Fraction fraction2, Fraction fraction8)
{
int lcd = GetLCD(fraction8, fraction2);
int x = lcd/fraction8.Denominator;
int n = lcd/fraction2.Denominator;
int f2num = fraction2.Numerator*n;
int f8num = fraction8.Numerator*x;
int t = fraction2.Numerator;
Fraction Fraction3 = new Fraction(f2num+f8num,lcd);
return Fraction3;
}
public int GetLCD(Fraction b, Fraction c)
{
int i = b.Denominator;
int j = c.Denominator;
while (true)
{
if (i == j)
{
return i;
}
j = j + j;
i = i + i;
}
}
}
It didn't make sense to have GetLCD, Add & Subtract methods in the class. So, I moved it out of the class and made them static methods.
Your GetLCD function doesn't get LCD correctly. This will give you the required result.(I didn't bother to make the Subtract method working, you can follow the below code & make it work yourself)
PS: I didn't change all of your variable names & I would recommend you to make them as meaningful as possible. n,z,x,y,b,c are not good variable names
static void Main(string[] args)
{
Fraction n = new Fraction(2, 4);
Fraction z = new Fraction(3, 12);
Fraction sum = Add(z, n);
int x = sum.Numerator;
int y = sum.Denominator;
Console.WriteLine("{0}/{1}", x, y);
Console.ReadKey(true);
}
public static Fraction Add(Fraction fraction2, Fraction fraction8)
{
int lcd = GetLCD(fraction8, fraction2);
int multiplier = 0;
if (fraction2.Denominator < lcd)
{
multiplier = lcd / fraction2.Denominator;
fraction2.Numerator = multiplier * (fraction2.Numerator);
fraction2.Denominator = multiplier * (fraction2.Denominator);
}
else
{
multiplier = lcd / fraction8.Denominator;
fraction8.Numerator = multiplier * (fraction8.Numerator);
fraction8.Denominator = multiplier * (fraction8.Denominator);
}
Fraction Fraction3 = new Fraction(fraction2.Numerator + fraction8.Numerator, lcd);
return Fraction3;
}
public static int GetLCD(Fraction b, Fraction c)
{
int i = b.Denominator;
int j = c.Denominator;
int greater = 0;
int lesser = 0;
if (i > j)
{
greater = i; lesser = j;
}
else if (i < j)
{
greater = j; lesser = i;
}
else
{
return i;
}
for (int iterator = 1; iterator <= lesser; iterator++)
{
if ((greater * iterator) % lesser == 0)
{
return iterator * greater;
}
}
return 0;
}
internal class Fraction
{
public Fraction(int numerator, int denominator)
{
Numerator = numerator;
Denominator = denominator;
}
public int Numerator { get; set; }
public int Denominator { get; set; }
}
Personally, I think your first mistake is trying to calculate the Lowest Common Denominator instead of just finding the simplest common denominator. Finding the LCD is a great stratgety for humans working this out on paper because of pattern recognition: we can recognize LCDs quickly; but calculating the LCD, and then converting the fractions to it is significantly more steps for a computer that must perform every step every time (and is not able to recognize patterns). Plus, once you add the two fractions after transforming them to their LCD it isn't even guaranteed to be a reduced result. I'm assuming that the reduced result is required, as is usually expected with fractional arithmetic. And because it seems useful, I put the reduction directly into the constructor code:
internal class Fraction
{
public Fraction(int numerator, int denominator, bool reduce = false)
{
if (!reduce)
{
Numerator = numerator;
Denominator = denominator;
}
else
{
var GCD = GreatestCommonDivisor(numerator, denominator);
Numerator = numerator / GCD;
Denominator = denominator / GCD;
}
}
public int Numerator { get; private set; }
public int Denominator { get; private set; }
public static Fraction Add(Fraction first, Fraction second)
{
return Combine(first, second, false);
}
public static Fraction Subtract(Fraction first, Fraction second)
{
return Combine(first, second, true);
}
private static Fraction Combine(Fraction first, Fraction second, bool isSubtract)
{
var newDenominator = first.Denominator * second.Denominator;
var newFirst = first.Numerator * second.Denominator;
var newSecond = first.Denominator * second.Denominator;
if (isSubtract)
{
newSecond = newSecond * -1;
}
return new Fraction(newFirst + newSecond, newDenominator, true);
}
private static int GreatestCommonDivisor(int a, int b)
{
return b == 0 ? a : GreatestCommonDivisor(b, a % b);
}
}
Edit: stole Greatest Common Divisor code from this answer

Solving Mathematical logics, placing digits in third number from digits of first and second number

I have two numbers.
First Number is 2875 &
Second Number is 852145
Now I need a program which create third number.
Third Number will be 2885725145
The logic is
First digit of third number is first digit of first number.
Second digit of third number is first digit of second number.
Third digit of third number is second digit of first number.
Fourth digit of third number is second digit of second number;
so on.
If any number has remaining digits then that should be appended at last.
I do not want to convert int to string.
int CreateThirdNumber(int firstNumber, int secondNumber)
{
}
So can anyone suggest me any solution to this problem?
I do not want to convert int to string.
Why?
Without converting to string
Use Modulus and Division operator.
With converting to string
Convert them to string. Use .Substring() to extract and append value in a string. Convert appended string to integer.
Here's a bit that will give you a lead:
Say you have the number 2875. First, you need to determine it's length, and then, extract the first digit
This can be easily calculated:
int iNumber = 2875;
int i = 10;
int iLength = 0;
while (iNumber % i <= iNumber){
iLength++;
i *= 10;
}
// iNumber is of length iLength, now get the first digit,
// using the fact that the division operator floors the result
int iDigit = iNumber / pow(10, iLength-1);
// Thats it!
First a little advice: if you use int in C#, then the value in your example (2885725145) is bigger than int.MaxValue; (so in this case you should use long instead of int).
Anyway here is the code for your example, without strings.
int i1 = 2875;
int i2 = 852145;
int i3 = 0;
int i1len = (int)Math.Log10(i1) + 1;
int i2len = (int)Math.Log10(i2) + 1;
i3 = Math.Max(i1, i2) % (int)Math.Pow(10, Math.Max(i1len, i2len) - Math.Min(i1len, i2len));
int difference = (i1len - i2len);
if (difference > 0)
i1 /= (int)Math.Pow(10, difference);
else
i2 /= (int)Math.Pow(10, -difference);
for (int i = 0; i < Math.Min(i1len, i2len); i++)
{
i3 += (i2 % 10) * (int)Math.Pow(10, Math.Max(i1len, i2len) - Math.Min(i1len, i2len) + i * 2);
i3 += (i1 % 10) * (int)Math.Pow(10, Math.Max(i1len, i2len) - Math.Min(i1len, i2len) + i * 2 + 1);
i1 /= 10;
i2 /= 10;
}
I don't understand why you don't want to use strings (is it homework?). Anyway this is another possible solution:
long CreateThirdNumber(long firstNumber, long secondNumber)
{
long firstN = firstNumber;
long secondN = secondNumber;
long len1 = (long)Math.Truncate(Math.Log10(firstNumber));
long len2 = (long)Math.Truncate(Math.Log10(secondNumber));
long maxLen = Math.Max(len1, len2);
long result = 0;
long curPow = len1 + len2 + 1;
for (int i = 0; i <= maxLen; i++)
{
if (len1 >= i)
{
long tenPwf = (long)Math.Pow(10, len1 - i);
long firstD = firstN / tenPwf;
firstN = firstN % tenPwf;
result = result + firstD * (long)Math.Pow(10, curPow--);
}
if (len2 >= i)
{
long tenPws = (long)Math.Pow(10, len2 - i);
long secondD = secondN / tenPws;
result = result + secondD * (long)Math.Pow(10, curPow--);
secondN = secondN % tenPws;
}
}
return result;
}
This solves it:
#include <stdio.h>
int main(void)
{
int first = 2875,second = 852145;
unsigned int third =0;
int deci,evenodd ,tmp ,f_dec,s_dec;
f_dec = s_dec =1;
while(first/f_dec != 0 || second/s_dec != 0) {
if(first/f_dec != 0) {
f_dec *=10;
}
if( second/s_dec != 0) {
s_dec *= 10;
}
}
s_dec /=10; f_dec/=10;
deci = s_dec*f_dec*10;
evenodd =0;tmp =0;
while(f_dec != 0 || s_dec !=0 ) {
if(evenodd%2 == 0 && f_dec !=0 ) {
tmp = (first/f_dec);
first -=(tmp*f_dec);
tmp*=deci;
third+=tmp;
f_dec/=10;
deci/=10;
}
if(evenodd%2 != 0 && s_dec != 0) {
tmp= (second/s_dec);
second -=(tmp*s_dec);
//printf("tmp:%d\n",tmp);
tmp*=deci;
third += tmp;
s_dec/=10;
deci/=10;
}
evenodd++;
}
printf("third:%u\ncorrct2885725145\n",third);
return 0;
}
output:
third:2885725145
corrct2885725145
#include <stdio.h>
long long int CreateThirdNumber(int firstNumber, int secondNumber){
char first[11],second[11],third[21];
char *p1=first, *p2=second, *p3=third;
long long int ret;
sprintf(first, "%d", firstNumber);
sprintf(second, "%d", secondNumber);
while(1){
if(*p1)
*p3++=*p1++;
if(*p2)
*p3++=*p2++;
if(*p1 == '\0' && *p2 == '\0')
break;
}
*p3='\0';
sscanf(third, "%lld", &ret);
return ret;
}
int main(){
int first = 2875;
int second = 852145;
long long int third;
third = CreateThirdNumber(first, second);
printf("%lld\n", third);
return 0;
}

round up given number in c#

I want round up given numbers in c#
Ex:
round(25)=50
round(250) = 500
round(725) = 1000
round(1200) = 1500
round(7125) = 7500
round(8550) = 9000
Most of your data suggests that you want to round up to the nearest multiple of 500. This would be done by
int round(int input)
{
return (int)(500 * Math.Ceiling(input / 500.0));
}
The rounding of 25 to 50 will not work, though.
Another guess would be that you want your rounding to depend on the size of the number being rounded. The following function would round 25 to 50, 250 to 500, 0.025 to 0.05, and 2500 to 5000. Maybe you can work from there.
double round(double input)
{
double scale = Math.Floor(Math.Log10(input));
double step = 5 * Math.Pow(10, scale);
return step * Math.Ceiling(input/step);
}
Depending on what you need, this could be a nice, reusable solution.
static int RoundUpWeird(int rawNr)
{
if (rawNr < 100 && rawNr > -100)
return RoundUpToNext50(rawNr);
else
return RoundUpToNext500(rawNr);
}
static int RoundUpToNext50(int rawNr)
{
return RoundUpToNext(rawNr, 50);
}
static int RoundUpToNext500(int rawNr)
{
return RoundUpToNext(rawNr, 500);
}
static int RoundUpToNext(int rawNr, int next)
{
int result;
int remainder;
if ((remainder = rawNr % next) != 0)
{
if (rawNr >= 0)
result = RoundPositiveToNext(rawNr, next, remainder);
else
result = RoundNegativeToNext(rawNr, remainder);
if (result < rawNr)
throw new OverflowException("round(Number) > Int.MaxValue!");
return result;
}
return rawNr;
}
private static int RoundNegativeToNext(int rawNr, int remainder)
{
return rawNr - remainder;
}
private static int RoundPositiveToNext(int rawNr, int next, int remainder)
{
return rawNr + next - remainder;
}
This code should work according to the rules I can gather:
public static double Round(double val)
{
int baseNum = val <= 100 ? 100 : 1000;
double factor = 0.5;
double v = val / baseNum;
var res = Math.Ceiling(v / factor) / (1 / factor) * baseNum;
return res;
}
This should work. And also for numbers greater than those you wrote:
int round(int value)
{
int i = 1;
while (value > i)
{
i *= 10;
}
return (int)(0.05 * i * Math.Ceiling(value / (0.05 * i)));
}

Categories

Resources