Making formulas in c# to convert dollar amount into denominations

Making formulas in c# to convert dollar amount into denominations



I'm making a program to convert any input amount into denominations of twenties, tens, fives, and ones. Here's what I'm having trouble with:


int twenties = dollar/20;
int tens = twenties/2;
int fives = tens/2;
int ones = fives/5;



Dollar is the input amount, the twenties expression seems to be the only one that comes out right. For example: I input 161 and it comes out as 161 is equal to 8 twenties, 4 tens, 2 fives, and 0 ones. But it should be coming out as 161 is equal to 8 twenties, 0 tens, 0 fives, and 1 ones. I know there is a really simple answer to this but I'm just not getting it, thanks guys.






You are asking about C#?

– Yunnosch
Sep 13 '18 at 4:45






You shouldn't divide the twenties by 2 but whatever amount is left after you took the twenties away.

– tkausl
Sep 13 '18 at 4:48






How do you know that it should be 0 tens?

– Yunnosch
Sep 13 '18 at 4:48






Because your title clearly says "C#", I changed your tags from "C" to "C#". Let me know if you do not agree.

– Yunnosch
Sep 13 '18 at 4:52





7 Answers
7



You need to work with the remainder after removing those denominations. The easiest way to get the remainder is the modulus operator, like this...


int dollar = 161;
int twenties = dollar / 20;
int remainder = dollar % 20;
int tens = remainder / 10;
remainder = remainder % 10;
int fives = remainder / 5;
remainder = remainder % 5;
int ones = remainder;



The above approach does not modify the original amount. By refactoring that into a method, it makes it easier to reuse with different denominations:


public int RemoveDenomination(int denomination, ref int amount)

int howMany = amount / denomination;
amount = amount % denomination;
return howMany;



...which you could use like this...


int dollar = 161;
int hundreds = RemoveDenomination(100, ref dollar);
int fifties = RemoveDenomination(50, ref dollar);
int twenties = RemoveDenomination(20, ref dollar);
int tens = RemoveDenomination(10, ref dollar);
int fives = RemoveDenomination(5, ref dollar);
int ones = dollar;



This approach does modify the dollar value. So if you don't want to change it, make a copy of it in another variable, and work on the copy.


dollar



You have to use the remainder and subtract until the remainder becomes 0;


int amount = 161, temp = 0;
int denomination = 20, 10, 5, 1 ; // you can use enums also for
// readbility
int count = new int[denomination.Length];

while (amount > 0)

count[temp] = amount / denomination[temp];
amount -= count[temp] * denomination[temp];
temp ++;



Another option is to use linq:


int denominations = new 20, 10, 5, 1 ;
List<int> result =
denominations
.Aggregate(new Result = new List<int>(), Remainder = 161 , (a, x) =>

a.Result.Add(a.Remainder / x);
return new a.Result, Remainder = a.Remainder % x ;
)
.Result;



That returns a list with the values 8, 0, 0, 1 .


8, 0, 0, 1



Alternatively you could do this:


public static Dictionary<string, int> Denominations(int amount)

var denominations = new Dictionary<string, int>();

denominations["twenties"] = amount / 20;
amount = amount % 20;

denominations["tens"] = amount / 10;
amount = amount % 10;

denominations["fives"] = amount / 5;
amount = amount % 5;

denominations["ones"] = amount;

return denominations;



This console app illustrates a possible solution:


internal static class Program

internal static void Main()

var results = GetDenominationValues();
foreach (var result in results)

Console.WriteLine($"result.Key - result.Value");


Console.ReadLine();


private static Dictionary<int, int> GetDenominationValues()

var amount = 161;
var denominations = new 20, 10, 5, 1 ;
var results = new Dictionary<int, int>();

foreach (var denomination in denominations)

results.Add(denomination, amount / denomination);
amount = amount % denomination;

if (amount == 0) break;


return results;




The key difference is in the line:


amount = amount % denomination;



Which uses the remainder of the previous division in the subsequent calculations.



If the division is greater than zero pass on the remainder (using the modulo operator, %) else pass on the original value to the next division in line. Repeat until no more division left.
Also, divide tens with 10, fives with 5 and ones with 1.


%


tens


fives


ones



Please use below function that returns dictionary


public static Dictionary<string, int> Denominations(int amount)

Dictionary<string, int> denominations = new Dictionary<string, int>() "twenties", 0 , "tens", 0 , "fives", 0 , "ones", 0 ;

int twenties, tens, fives, ones;
if (amount >= 20)

twenties = amount/20;
denominations["twenties"] = twenties;
amount -= twenties * 20;

if (amount >= 10)

tens = amount/10;
denominations["tens"] = tens;
amount -= tens * 10;

if (amount >= 5)

fives = amount/5;
denominations["fives"] = fives;
amount -= fives * 5;


if (amount >= 1)

ones = amount;
denominations["ones"] = ones;



return denominations;






Did you find any mistake in the code, so vote down this code

– Uttam Gupta
Sep 13 '18 at 5:26







The code doesn't compile.

– Enigmativity
Sep 13 '18 at 6:20






Enigmativity, i just shared logic part only, as user posted his logic instead of entire code.

– Uttam Gupta
Sep 13 '18 at 6:39






That's not the way this site works. You should always strive for working answers.

– Enigmativity
Sep 13 '18 at 6:42






thanks Enigmativity, i have updated the code

– Uttam Gupta
Sep 13 '18 at 7:04



You want Code number to text ?


MoneyExt mne = new MoneyExt();
string textamount;
textamount = mne.ConvertNumberToENG(10000);

using System;

public class MoneyExt

public string ConvertNumberToENG(int amount)

var dollars, cents, temp;
var decimalPlace, count;
string place = new string[10];
place[2] = " Thousand ";
place[3] = " Million ";
place[4] = " Billion ";
place[5] = " Trillion ";

// String representation of amount.
amount = amount.Trim();
amount = amount.Replace(",", "");
// Position of decimal place 0 if none.
decimalPlace = amount.IndexOf(".");
// Convert cents and set string amount to dollar amount.
if (decimalPlace > 0)

cents = GetTens(ref amount.Substring(decimalPlace + 1).PadRight(2, "0").Substring(0, 2));
amount = amount.Substring(0, decimalPlace).Trim();


count = 1;
while (amount != "")

temp = GetHundreds(amount.Substring(Math.Max(amount.Length, 3) - 3));
if (temp != "")
dollars = temp + place[count] + dollars;
if (amount.Length > 3)
amount = amount.Substring(0, amount.Length - 3);
else
amount = "";
count = count + 1;


switch (dollars)

case "One":

dollars = "One Bath";
break;


default:

dollars = dollars + " Bath";
break;



// Select Case cents
// ' Case ""
// ' cents = " and No Cents"
// Case "One"
// cents = " and One Satang"
// Case Else
// cents = " and " & cents & " Satang"
// End Select

ConvertNumberToENG = dollars + cents;


// Converts a number from 100-999 into text
public string GetHundreds(string amount)

string Result;
if (!int.Parse(amount) == 0)

amount = amount.PadLeft(3, "0");
// Convert the hundreds place.
if (amount.Substring(0, 1) != "0")
Result = GetDigit(ref amount.Substring(0, 1)) + " Hundred ";
// Convert the tens and ones place.
if (amount.Substring(1, 1) != "0")
Result = Result + GetTens(ref amount.Substring(1));
else
Result = Result + GetDigit(ref amount.Substring(2));
GetHundreds = Result;



// Converts a number from 10 to 99 into text.
private string GetTens(ref string TensText)

string Result;
Result = ""; // Null out the temporary function value.
if (TensText.StartsWith("1"))

switch (int.Parse(TensText))

case 10:

Result = "Ten";
break;


case 11:

Result = "Eleven";
break;


case 12:

Result = "Twelve";
break;


case 13:

Result = "Thirteen";
break;


case 14:

Result = "Fourteen";
break;


case 15:

Result = "Fifteen";
break;


case 16:

Result = "Sixteen";
break;


case 17:

Result = "Seventeen";
break;


case 18:

Result = "Eighteen";
break;


case 19:

Result = "Nineteen";
break;


default:

break;



else

switch (int.Parse(TensText.Substring(0, 1)))

case 2:

Result = "Twenty ";
break;


case 3:

Result = "Thirty ";
break;


case 4:

Result = "Forty ";
break;


case 5:

Result = "Fifty ";
break;


case 6:

Result = "Sixty ";
break;


case 7:

Result = "Seventy ";
break;


case 8:

Result = "Eighty ";
break;


case 9:

Result = "Ninety ";
break;


default:

break;


Result = Result + GetDigit(ref TensText.Substring(1, 1)); // Retrieve ones place.

GetTens = Result;


// Converts a number from 1 to 9 into text.
private string GetDigit(ref string Digit)

switch (int.Parse(Digit))

case 1:

GetDigit = "One";
break;


case 2:

GetDigit = "Two";
break;


case 3:

GetDigit = "Three";
break;


case 4:

GetDigit = "Four";
break;


case 5:

GetDigit = "Five";
break;


case 6:

GetDigit = "Six";
break;


case 7:

GetDigit = "Seven";
break;


case 8:

GetDigit = "Eight";
break;


case 9:

GetDigit = "Nine";
break;


default:

GetDigit = "";
break;









While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find how to write a good answer very helpful. Please edit your answer.

– hellow
Sep 13 '18 at 6:10






"You want Code number to text ?" - No, they want to break down an amount into the notes and coins.

– Enigmativity
Sep 13 '18 at 6:18



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)