Currency dispaly tip

August 10, 2007 on 1:09 pm | In DotNet |

Task: print a price according to a culture information but remember “we don’t need the damn ? (or $) sign”!

Well:

        public static string FormatCurrency(double price)
        {
            CultureInfo ci = new CultureInfo(“de-DE”);
            NumberFormatInfo nfi = ci.NumberFormat;
            nfi.CurrencySymbol = “”;
            string s = price.ToString(“c”, nfi).Trim();
            return s;
        }

        public static double FromCurrency(string price)
        {
            CultureInfo ci = new CultureInfo(“de-DE”);
            NumberFormatInfo nfi = ci.NumberFormat;
            nfi.CurrencySymbol = “”;
            double d;
            double.TryParse(price, NumberStyles.Currency, nfi, out d);
            return d;
        }

An please print the percentage but without the damn % and ALWAYS with 5 decimal digits.

        public static string FormatInterest(double interest)
        {
            CultureInfo ci = new CultureInfo(“de-DE”);
            NumberFormatInfo nfi = ci.NumberFormat;
            nfi.NumberDecimalDigits = 5;
            string s = interest.ToString(“n”, nfi).Trim();
            return s;
        }

        public static double FromInterest(string interest)
        {
            CultureInfo ci = new CultureInfo(“de-DE”);
            NumberFormatInfo nfi = ci.NumberFormat;
            double d;
            double.TryParse(interest, NumberStyles.Number, nfi, out d);
            return d;
        }

PS. Prase the Windows Live Writer and VSPaste plugin (it lets you paste the Visual Studio code snippets with proper text highlighting).

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds. Valid XHTML and CSS. ^Top^