Wednesday, 29 January 2014

Decimal Manipulation in C#

Leave a Comment
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 =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.