Re: Data format constraint
There are a couple of ways of doing what you want... either format the number as you want it displayed, or use a Masked Editbox to control the format of input/output...
For some formatting thoughts, see http://www.csharp-examples.net/string-format-double/
Gives a few examples and outputing a number in various ways... there are many more resources like that throughout the web... google "c# format double".
Best of luck!
Re: Data format constraint
'double' is the incorrect datatype to use for money. You should be using the decimal type. If you don't, then you'll run into rounding issues all the time which will throw the balance off.
As for displaying the correct value on the till, .ToString () won't do the job unfortunately as it doesn't enforce that only 2 decimal places will be displayed. You can probably use ToString ("{0:0.00}") which will enforce exactly two decimal places are used to display the number. I don't think this method rounds the value though, so 2.999 would show up as 2.99 instead of 3.00 as you might want.
Re: Data format constraint
Re: Data format constraint
I would make a "String DoubleToDollars(double amount)" function
Code:
double dDollars,dCents;
String sDollars,sCents;
dDollars=amount-floor(amount)
dCents=amount-Dollars
sDollars=dDollars.tostring();
if dCents==0
sCents="00";
else sCents=dCents.ToString();
//return "$"+sDollars+"."+sCents;
//add commas to for sDollars? $1,302.01 looks better than $1302.01?
When you want to control how money is displayed, then control how money is displayed! Hooray tautology!