SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Location
    A city equidistant from two coasts of India.
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Converting to two decimal places.

    I have following C# code:

    Code Csharp:
    Total = SubTotal - (SubTotal*ColumnPercent/100);

    All the value are initially declared as "Decimal". I want the Total to store values with two decimal places.

    For example, if the calculated value comes out to be: 2155.33333, Total must store: 2155.33

  2. #2
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If it's for currency try using:
    Code Csharp:
    Console.WriteLine("{0:c}", Total);
    for a console app or
    Code Csharp:
    String.Format("{0:c}", Total);
    for a web form.

    If not for currency ... umm, I forget. BRB.

    Edit:
    Code Csharp:
    static void Main(string[] args)
    {
    	Double Total;
    	Double SubTotal = 100;
    	Double ColumnPercent = 33.3333333333333;
     
    	Total = SubTotal - (SubTotal * ColumnPercent/100);
     
    	Console.WriteLine("{0}", Total);
    	Console.WriteLine("{0:f2}", Total);
     
    }

  3. #3
    Chopped Liver bronze trophy imaginekitty's Avatar
    Join Date
    Aug 2007
    Location
    Pennsyltucky
    Posts
    1,494
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    By the way "f" is the "fixed-point string format specifier. I believe 2 is the default so you may be able to leave it out and just use {0:f}.

  4. #4
    Chive On FFCus's Avatar
    Join Date
    Feb 2006
    Location
    Connecticut
    Posts
    542
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Though not as efficient as string.format, you can also use the FormatNumber function. That's a throwback to the days of ASP.OLD


Tags for this Thread

Bookmarks

Posting Permissions

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