C# point numbers

kensy11
kensy11 used Ask the Experts™
on
Hello,

i have made a program it ask for a money amount .
and it spits the money for example , If you give 150 its wil says:

1 X 100
1X 50

But when i try to use 150,20 cent

it doesn't work , i don't know what im doing wrong or what i need to do?


Program.cs
using System;

namespace ConsAppl1TI34 {
  class Program {
    static void Main(string[] args) {
      ConsCode Cc = new ConsCode();
      Cc.ExecuteProgram();
      Console.ReadKey(true);
    }/*Main*/
  }/*Program*/
}/*ConsAppl1TI34*/

Open in new window


ConsCode.cs
using System;

namespace ConsAppl1TI34 {
  class ConsCode {
      public void ExecuteProgram() {
      decimal Bedrag;
      uint Nr500, Nr200, Nr100, Nr50, Nr20, Nr10, Nr5, Nr2, Nr1, Nr50c, Nr20c, Nr10c, Nr5c, Nr2c, Nr1c;
      ClsMoney Cm = new ClsMoney();
      Console.Title = "Opgave 10: Splitsing geldbedrag (zonder tabel)";
      Bedrag = GiveAmount();
      Cm.SplitAmount(Bedrag, out Nr500, out Nr200, out Nr100, out Nr50, out Nr20, out Nr10, out Nr5,
      out Nr2, out Nr1, out Nr50c, out Nr20c, out Nr10c, out Nr5c, out Nr2c, out Nr1c);
      DisplayResult(Bedrag, Nr500, Nr200, Nr100, Nr50, Nr20, Nr10, Nr5,
      Nr2, Nr1, Nr50c, Nr20c, Nr10c, Nr5c, Nr2c, Nr1c);
      }/*ExecuteProgram*/

      public void DisplayResult(decimal Bedrag, uint Nr500, uint Nr200, uint Nr100, uint Nr50, uint Nr20, uint Nr10, uint Nr5, uint Nr2, uint Nr1, uint Nr50c, uint Nr20c, uint Nr10c, uint Nr5c, uint Nr2c, uint Nr1c) {
        Console.WriteLine("Een bedrag van {} Euro wordt het kortst verdeeld als:");


        Console.WriteLine("{0} X 500", Nr500);
        Console.WriteLine("{0} X 200", Nr200);
        Console.WriteLine("{0} X 100", Nr100);
        Console.WriteLine("{0} X 50", Nr50);
        Console.WriteLine("{0} X 20", Nr20);
        Console.WriteLine("{0} X 10", Nr10);
        Console.WriteLine("{0} X 5", Nr5);
        Console.WriteLine("{0} X 2", Nr2);
        Console.WriteLine("{0} X 1", Nr1);
        Console.WriteLine("{0} X 0,50", Nr50c);
        Console.WriteLine("{0} X 0,20", Nr20c);
        Console.WriteLine("{0} X 0,10", Nr10c);
        Console.WriteLine("{0} X 0,05", Nr5c);
        Console.WriteLine("{0} X 0,02", Nr2c);
        Console.WriteLine("{0} X 0,01", Nr1c);

      }
      public decimal GiveAmount()
      {
 	      Console.WriteLine("Geef een bedrag in euro in a.u.b ...");
        decimal GiveAmount = decimal.Parse(Console.ReadLine());
        return GiveAmount;
      }/*GiveAmount*/
  }/*ConsCode*/
}/*ConsAppl1TI34*/

Open in new window



ClsMoney.cs
using System;

namespace ConsAppl1TI34 {
  class ClsMoney {

    public void SplitAmount(decimal Bedrag, out uint Nr500, out uint Nr200, out uint Nr100, out uint Nr50, out uint Nr20, out uint Nr10, out uint Nr5,
      out uint Nr2, out uint Nr1, out uint Nr50c, out uint Nr20c, out uint Nr10c, out uint Nr5c, out uint Nr2c, out uint Nr1c){
      
      Nr500 = 0;
      Nr200 = 0;
      Nr100 = 0;
      Nr50 = 0;
      Nr20 = 0;
      Nr10 = 0;
      Nr5 = 0;
      Nr2 = 0;
      Nr1 = 0;
      Nr50c = 0;
      Nr20c = 0;
      Nr10c = 0;
      Nr5c = 0;
      Nr2c = 0;
      Nr1c = 0;

        while (Bedrag > 500) {

          Nr500 = Convert.ToUInt32(Math.Floor(Bedrag / 500));
          Bedrag = Bedrag % 500;
        }

        while (Bedrag >= 200) {

          Nr200 = Convert.ToUInt32(Math.Floor(Bedrag / 200));
          Bedrag = Bedrag % 200;
        }

        while (Bedrag >= 100) {

          Nr100 = Convert.ToUInt32(Math.Floor(Bedrag / 100));
          Bedrag = Bedrag % 100;
        }

        while (Bedrag >= 50) {

          Nr50 = Convert.ToUInt32(Math.Floor(Bedrag / 50));
          Bedrag = Bedrag % 50;
        }

        while (Bedrag >= 20) {

          Nr20 = Convert.ToUInt32(Math.Floor(Bedrag / 20));
          Bedrag = Bedrag % 20;
        }
        while (Bedrag >= 10) {

          Nr10 = Convert.ToUInt32(Math.Floor(Bedrag / 10));
          Bedrag = Bedrag % 10;
        }
        while (Bedrag >= 5) {

          Nr5 = Convert.ToUInt32(Math.Floor(Bedrag / 5));
          Bedrag = Bedrag % 5;
        }
        while (Bedrag >= 2) {

          Nr2 = Convert.ToUInt32(Math.Floor(Bedrag / 2));
          Bedrag = Bedrag % 2;
        } while (Bedrag >= 1) {

          Nr1 = Convert.ToUInt32(Math.Floor(Bedrag / 1));
          Bedrag = Bedrag % 1;
        } while (Bedrag >= 1) {

          Nr1 = Convert.ToUInt32(Math.Floor(Bedrag / 1));
          Bedrag = Bedrag % 1;
        }
        while (Bedrag >= 0.50m) {

          Nr50c = Convert.ToUInt32(Bedrag / 0.50m);
          Bedrag = Bedrag % 1;
          
        }
    }
  }
}

Open in new window

You can download the program

thank you
Opgave-02.zip
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Top Expert 2009
Commented:
Your current code is not calculating cents correctly there is no code for 20c, it always return 0.

At ClsMoney.SplitAmount method replace:
        while (Bedrag >= 0.50m) {

          Nr50c = Convert.ToUInt32(Bedrag / 0.50m);
          Bedrag = Bedrag % 1;
         
        }

with this code:
        Bedrag = Bedrag * 100;
        while (Bedrag >= 50) {

          Nr50c = Convert.ToUInt32(Math.Floor(Bedrag / 50));
          Bedrag = Bedrag % 50;
        }
        while (Bedrag >= 20) {

          Nr20c = Convert.ToUInt32(Math.Floor(Bedrag / 20));
          Bedrag = Bedrag % 20;
        }
        while (Bedrag >= 10) {

          Nr10c = Convert.ToUInt32(Math.Floor(Bedrag / 10));
          Bedrag = Bedrag % 10;
        }
        while (Bedrag >= 5) {

          Nr5c = Convert.ToUInt32(Math.Floor(Bedrag / 5));
          Bedrag = Bedrag % 5;
        }
        while (Bedrag >= 1) {

          Nr1c = Convert.ToUInt32(Math.Floor(Bedrag / 1));
          Bedrag = Bedrag % 1;
        }

Note: Why are are using "," to represent decimal point at 150,20. My guess is that is your cultural settings to represent the double number. Is this correct?

Author

Commented:
Yes we use , instead of .

but how can i use '.' point when i use point instead of a ',' i get something else

fore example when i give 150.20 it thinks its 15020 euro
but when i use ',' it does the tight thing so its 150 euro and 20 cent

Thank you
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
What culture is your development machine using? In other words, if you go to the "Region and Language" settings, what language does you machine display?

The problem is as mas_oz2003 mentions:  the culture of the input does not match the culture of the thread running the code (which is inferred from the culture of the machine by default). If I run this on my machine (using "en-US" for a culture), then when the decimal parsing code sees the comma, but not enough numbers after the comma (expected 3), then it simply strips it out. If I were to run this using a culture format which matched the expected culture (forgive me if I am incorrect, but I believe it is Dutch), then it should parse correctly.

If your machine is set up for Dutch, then the decimal.Parse logic should work correctly; however, if you are set up for a culture which does not use comma as the fractional separator (like "en-US"), then the parse logic will parse incorrectly. You can "hard-code" the culture to a specific one, but then if you distributed the application to someone who was not accustomed to that format, then their entries would fail for the same reason.

For example, if I fix the culture to Dutch in the Netherlands:

IFormatProvider format = System.Globalization.CultureInfo.GetCultureInfo("nl-NL").NumberFormat;
decimal GiveAmount = decimal.Parse(Console.ReadLine(), format);

Open in new window


...then as long as I enter the amount in the format "150,20", it will be parsed as if my machine was set to use Dutch for its culture. As I mentioned, though, I am in the U.S. "150,20" is not how I would write "150 dollars and 20 cents" (U.S. terminology); I would write "150.20". For me, hard-coding the culture would cause problems. If you know that only Dutch speakers will use your application, then it may be fine to hard-code the culture. Often, though, it is easier to let the machine the code is running on relay the culture information to the application.

Here's what the result of hard-coding the culture information looked like even though my machine is running as "en-US":

Screenshot
In the code portion of that screenshot you will notice that the tooltip display a decimal (full stop) between the "150" and the "20" even though what I entered actually was a comma. This is because the parse logic used the Dutch culture info I passed it to parse the input as Dutch, but since my machine is set up for "en-US" I see the input in a format that is customary for me.
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
I asked just to verify the code usage in case I miss a special case. (I tested with my current settings that uses the dot(.) and it was working fine anyway)
That is OK, you just confirmed that your regional settings use the comma to represent decimals.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial