Decimal Manipulation in C#

This post will  show manipulation on the Data Type Decimal.

  //This is the hard code value for decimal
  //without suffic m or M, the compiler thread the object as a double hence will cause error
  decimal price = 10.1654M;

  //Convert decimal to double
  double priceInDouble = Convert.ToDouble(price); //output : 10.1654
  priceInDouble = (double)price;//output : 10.1654

  //convert to double with 2 decimal point,        
  priceInDouble = Math.Round((double)price, 2); // output : 10.17

  //convert back to decimal
  price = Convert.ToDecimal(priceInDouble);//output : 10.17
  price = (decimal)priceInDouble;//output : 10.17

  //convert decimal to currency. The currency format will depend on the region,
  //like myself in malaysia compiler will automatically give "RM" as a currency format
  string currency = String.Format("Order Total: {0:C}", price); //output :RM10.17
  currency = String.Format("Order Total: {0:C2}", price); //output :RM10.17 ,2 decimal point
  currency = String.Format("Order Total: {0:C3}", price); //output :RM10.170 , 3 decimal point
  currency = String.Format("Order Total: {0:C4}", price); //output :RM10.1700 ,4 decimal point


By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

How to create zip file to download in JSP- Servlet

How to create DataGrid or GridView in JSP - Servlet

Pinging in ASP.NET Example