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();






    }
Share:
17,097
armin
Author by

armin

Updated on June 05, 2022

Comments

  • armin
    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
    Olhovsky about 13 years
    The important reason to use Decimal for money: stackoverflow.com/questions/316727/…
  • George Stocker
    George Stocker about 13 years
    @TheBigO yup, I actually linked to that in a now deleted answer given below by another user.
  • armin
    armin about 13 years
    Should we use array for it ?
  • Joe
    Joe about 13 years
    well, depends on how many of these you need. you can create a List<Money> money = new List<Money>(); to store alot of these
  • armin
    armin about 13 years
    console.readline( frome user), console.writeline( you have X cent ,or x Dollar and x cent)
  • Joe
    Joe about 13 years
    are 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
    Joe about 13 years
    Please edit your inital question to ask exactly what you need. include as much info as possible so we can help you.
  • armin
    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
    Joe about 13 years
    ok, 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
    Joe about 13 years
    no 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
    armin about 13 years
    Oh 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
    Admin about 13 years
    Actually, 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
    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
    Admin about 13 years
    A 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
    William T. Mallard almost 8 years
    Formatting seems wrong for cents < 10, i.e. "$1.1"?