How do I format cents and dollars in C#? How do I convert one to the other?
17,097
Solution 1
You should probably use the Decimal
datatype, and then instead of trying to convert cents to dollars, use one standard notation:
Decimal amount = .25M;
String.Format("Amount: {0:C}", amount);
The output is: Amount: $0.25
;
Solution 2
class Money
{
public int Dollar {get; set;}
public int Cent { get; set;}
public Money(int cents)
{
this.Dollar = Math.Floor(cents/100);
this.Cent = cents%100;
}
}
and u can use it like so
int cents = Convert.ToInt32(Console.Readline("Please enter cents to convert:"))
Money money = new Money(cents);
Console.Writeline("$" + money.Dollar + "." + money.Cent);
Solution 3
I think this is Best Answer I write with Array
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please input your cent or dollar");
int coins = int.Parse(Console.ReadLine());
int[] dollars = new int[2];
dollars[0] = coins / 100;
dollars[1] = coins % 100;
Console.WriteLine("{0} dollar and {1} coins", dollars[0], dollars[1]);
Console.ReadLine();
}
Author by
armin
Updated on June 05, 2022Comments
-
armin almost 2 years
If I have something written as 25 cents in either C# or Java, how would I convert that to read $.25?
-
Olhovsky about 13 yearsThe important reason to use Decimal for money: stackoverflow.com/questions/316727/…
-
George Stocker about 13 years@TheBigO yup, I actually linked to that in a now deleted answer given below by another user.
-
armin about 13 yearsShould we use array for it ?
-
Joe about 13 yearswell, depends on how many of these you need. you can create a List<Money> money = new List<Money>(); to store alot of these
-
armin about 13 yearsconsole.readline( frome user), console.writeline( you have X cent ,or x Dollar and x cent)
-
Joe about 13 yearsare you asking how to incorporate console.readline and writeline into it? or stating that you can do it? or asking if you need an array for readline and writeline? what's your final goal? to parse a list of cents or to get cents from a user one at a time and print it out straight away?
-
Joe about 13 yearsPlease edit your inital question to ask exactly what you need. include as much info as possible so we can help you.
-
armin about 13 years@Joe ,thanks dear I mean I want a program , when user give its cent its convert to Dollars . example : 100 cent = 1 $ dollar or another example 125 cent = 1 $ dollar and 25 cent ?
-
Joe about 13 yearsok, so i've included the readline, user enters the amount, then it's converted and printed out, and you have the money object just in case you want to store it. still don't know what you want with an array... you don't need an array for this.
-
Joe about 13 yearsno worries, glad to help :) although next time, just say, i want a full program that will take in cents and output dollars and cents.
-
armin about 13 yearsOh dear so thsnks .but I try to write this code with arry but I got a lot probelm . thanks again for your time and attention . Sir have a good night ,thanks in advance
-
Admin about 13 yearsActually, I would argue against Decimal for money (and rather a specific monetary type including "all the details support"), but it's a good bit better than using a float -- it still is relative precision and can, in degenerate cases or extremes, exhibit the same "problems" as using a float.
-
George Stocker about 13 years@pst Microsoft seems to think it's suitable. msdn.microsoft.com/en-us/library/364x0z75%28v=vs.80%29.aspx
-
Admin about 13 yearsA very contrived case:
decimal m = Decimal.MaxValue - 1; (m / 3m * 3m) == m; // false
(although I suppose this is akin to saying that GUIDs will "run out" soon). Also the rules for dealing the money can be rather complex (who gets the change in a split?) and there may be a smallest monetary unit. Of course this post/question is well below those scopes. -
William T. Mallard almost 8 yearsFormatting seems wrong for cents < 10, i.e. "$1.1"?