Custom Percentage and Currency formatting in C#

I am retreiving following decimal values from another class in C# and  need to convert these values into string.

 //Displaying column values as Percentage


col1: Getting value as 0.0777 and should display in the UI as  7.77%

col2: Getting value as 0.08 and should display in the UI as   8.00%

col3: Getting value as 0.9   and should display in the UI as  90.00%


//Displaying column values as Currency


Col4: Getting value as 2000 should display in the UI as $2000
col5:Getting value as 8000 should display in the UI as  $8000


Is it possible to do something like this:

String PercentageCustomFormat="{PercentageCustomFormat}";
string CurrencyCustomFormat="{CurrencyCustomFormat}";


The  PercentageCustomFormat/ CurrencyCustomFormat should contan logic that if any column returns Null,it should display as "NA".

Retrieving:

String.Format("{PercentageCustomFormat}", col1);

String.Format("{CurrencyCustomFormat}", col4);
ksd123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel Van Der WerkenIndependent ConsultantCommented:
I believe "standard" formats exist for what you want to do:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

So, to print out a percentage: string.Format({"0:P"}, col1);
and currency: string.Format({"0:C"}, col4);

Also, there are custom formats available:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
0
ksd123Author Commented:
Dan7el,

I am aware of above formats for currency and percentage, but I have dozen columns and looking for custom format (currency/percentage) for my issue.
0
Robberbaron (robr)Commented:
to include 'logic' I would will need code. Another class perhaps ?
            SpecialFormat sfmt = new SpecialFormat(24.6);
            Debug.WriteLine(sfmt.Percentage);
            Debug.WriteLine(sfmt.Currency);
            sfmt.Formatter = "{0:$#,##0;($#,##0);'empty'}";
            Debug.WriteLine(sfmt.Currency);
            sfmt = new SpecialFormat(null);
            Debug.WriteLine(sfmt.Percentage);
            Debug.WriteLine(sfmt.Currency);

Open in new window

2,460.00%
$24.60
$25
NA
NA
    class SpecialFormat
    {
        double? _myValue = null;
        public SpecialFormat(double? rawValue)
        {
            _myValue = rawValue;
            Formatter = null;  //set to default
        }

        public string Formatter { get; set; }

        public string Percentage
        { 
            get { 
                if( _myValue == null) {return "NA"; }
                if (Formatter == null)
                {
                    //use default format for percentage
                    return string.Format("{0:P}", _myValue); 
                }
                else
                {
                    //use custom formatter
                    return string.Format(Formatter, _myValue);
                    
                }
            }
        }

        public string Currency
        {
            get
            {
                if (_myValue == null) { return "NA"; }
                if (Formatter == null)
                {
                    //use default format for percentage
                    return string.Format("{0:C}", _myValue);
                }
                else
                {
                    //use custom formatter
                    return string.Format(Formatter, _myValue);

                }

            }
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim CakalicSenior Developer/ArchitectCommented:
Use the .NET DecimalFormat class. Documentation here:
http://msdn.microsoft.com/en-us/library/aa990162(v=vs.80).aspx

Regards,
Jim
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.