Currency dispaly tip

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).

Leave a Reply

Your email address will not be published. Required fields are marked *