CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2011
    Posts
    41

    Data format constraint

    I am working on a Point of sale module for a web application. I need help ensuring my data stays in a money format ie 0.00



    Here's my code:
    Code:
    01
                Double res = Double.Parse(PriceTxtBox.Text);
    02
                res = res * Double.Parse(QtyTxtBox.Text);
    03
                ItemTotalTxtBox.Text = res.ToString();
    04
     
    05
                if (ItemTotalTxtBox != null)
    06
                {
    07
                    Double rs = Double.Parse(ItemTotalTxtBox.Text);
    08
                    rs = (rs * (.075)) + Double.Parse(ItemTotalTxtBox.Text);
    09
     
    10
                    Double ts = (rs *(.075));
    11
     
    12
                    TotalTxtBox.Text = rs.ToString();
    13
                    TaxTxtBox.Text = ts.ToString();
    14
                }
    I can handle constraing the input on the client side.

    When PriceTxtBox is 2 and QtyTxtBox is 3 I need the result to be 6.00 not 6 like I am getting now.

    When ItemTotalTxtBox is 4.67 I need TotalTxtBox to be 0.35 not 0.35025 like I am getting now.



    Thanks in advance.

  2. #2
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    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!

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    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.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Data format constraint

    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Apr 2009
    Posts
    4

    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!
    Last edited by dennisbenson; July 5th, 2011 at 08:20 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured