2

If I have something written as 25 cents in either C# or Java, how would I convert that to read $.25?

George Stocker
  • 55,025
  • 29
  • 167
  • 231
armin
  • 39
  • 1
  • 1
  • 4

3 Answers3

9

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;

George Stocker
  • 55,025
  • 29
  • 167
  • 231
  • The important reason to use Decimal for money: http://stackoverflow.com/questions/316727/is-a-double-really-unsuitable-for-money – Olhovsky Feb 03 '11 at 03:12
  • @TheBigO yup, I actually linked to that in a now deleted answer given below by another user. – George Stocker Feb 03 '11 at 03:13
  • 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. –  Feb 03 '11 at 05:39
  • @pst Microsoft seems to think it's suitable. http://msdn.microsoft.com/en-us/library/364x0z75%28v=vs.80%29.aspx – George Stocker Feb 03 '11 at 13:39
  • 1
    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. –  Feb 03 '11 at 19:21
5
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);
Joe
  • 9,360
  • 6
  • 43
  • 56
  • Should we use array for it ? – armin Feb 03 '11 at 03:21
  • well, depends on how many of these you need. you can create a List money = new List(); to store alot of these – Joe Feb 03 '11 at 03:22
  • console.readline( frome user), console.writeline( you have X cent ,or x Dollar and x cent) – armin Feb 03 '11 at 03:24
  • 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 Feb 03 '11 at 03:27
  • 1
    Please edit your inital question to ask exactly what you need. include as much info as possible so we can help you. – Joe Feb 03 '11 at 03:28
  • @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 ? – armin Feb 03 '11 at 03:31
  • 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 Feb 03 '11 at 03:37
  • 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. – Joe Feb 03 '11 at 03:41
  • 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 – armin Feb 03 '11 at 03:47
  • Formatting seems wrong for cents < 10, i.e. "$1.1"? – William T. Mallard May 03 '16 at 16:27
0

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






    }
ali
  • 47
  • 1
  • 1
  • 4