Regulus's stupid C# questions thread

Soldato
Joined
18 Aug 2006
Posts
10,048
Location
ChCh, NZ
Ok, I started learning C# 5 days ago and have made very satisfactory progress. I keep struggling on silly little points but I managed to figure stuff out eventually. I wanted to have a thread whereby I can ask the experience guys on here some stuff I can't get working

I'm busy doing a project. I'm writing a ticketing system that sits on a SQL machine. I've installed the .NET framework in my 3 PC Lan and all the computers "talk" to the SQL machine. Imagine the PC's are the users ordering movie tickets, theather tickets etc. The program keeps track of how many tickets sold, what shows (there are only two),money earned and how many seats still left. It displays all the results to a datagrid and updates itself autamatically everytime a transaction is entered. It's very basic stuff and probably awful code but it works!

But, I have a silly problem. Laughable actually. I don't know how to display pennies. As in £6,23p.

My tickets cost 7.75 each, declared as integers. It refuses to display the pennies bit. Just throws an exception. I've tried double, float, decimal (which rounds it to the nearest £££). So if you use my system and pay with a tenner, you're pretty much screwed! Instead of getting back £2.25, you'll only get £2.00

I hope I explained this properly. Please be kind, 5 days experience here....
 
Im not sure if C# has a build in class to represent money. But what you can do is use a struct/class with two integer members to represent pounds and pence and write methods for arithmetic/display. However if you want to use a primative, Its likely that however you are outputting the money (String format) is rounding it (for this you need to see the docs for that function). Really need to see the code. And obviously you cannot store a floating point number in an integer (it will just truncate).
 
Last edited:
Writing structs is just a tad above my abilities at the moment.

Show me how you would write a program that will subtract 6.66 from 10 and display the result to a textbox.
 
Im not sure if C# has a build in class to represent money. But what you can do is use a struct/class with two integer members to represent pounds and pence and write methods for arithmetic/display.
:confused:What's wrong with a double?

Then using String.Format("{0:c}", myDouble) to present the number in the way you want.
 
regulus said:
Writing structs is just a tad above my abilities at the moment.

Show me how you would write a program that will subtract 6.66 from 10 and display the result to a textbox.

double num;

num = 10f - 6.66f;

myTextbox.Text = String.Format("{0:c}", num);
 
NathanE said:
double num;

num = 10f - 6.66f;

myTextbox.Text = String.Format("{0:c}", num);

Thanks!!! got it working now!

Although still don't understand the String.Format part. I'll just take it at face value for now
 
NathanE said:
:confused:What's wrong with a double?

Then using String.Format("{0:c}", myDouble) to present the number in the way you want.

Ok for common use you can get away with it "most" of the time. The main problem is rounding errors and loss of precision with floating point numbers.
Money is one of the area's where you really do not wanting to be loosing pence here and there :p

http://tinyurl.com/pyzm5 (Sorry google cache - website is down atm :/)
http://blogs.msdn.com/alfredth/archive/2006/03/17/show_me_the_money.aspx
 
Last edited:
Yup but remember the decimal type is software emulated.. hence slow.

"Truncating" the number to a specific layout can be done using String.Format, read up on that method.
 
Back
Top Bottom