Tuesday, March 17, 2009

decimal ToString

A few things that I have noticed when trying to convert decimal into string value:
1. If the decimal value is a nullable property and you want to format the string (.ToString("C"))=> you cannot do the following:

   SomeTextBox.Text = SubTotal.ToString("C");

   Where SubTotal is a nullable decimal property.

Instead you will have to do:

   //This will assign subTotal a value and therefore it is not nullable.
   var subTotal = SubTotal ?? 0.0m;
   SomeTextBox.Text = subTotal.ToString("C");

2. As far as string formating for a decimal value:
//This will display two decimal places
   subTotal.ToString("F");
//This will separate thousands
   subTotal.ToString("N");
//This will separate thousands and will precede with currency sign based on local environment.
   subTotal.ToString("C");

1 comment:

 
Watch the latest videos on YouTube.com