Sanjiv
Sanjiv

Reputation: 1298

Converting Indian Currency Symbol

I have a simple C# console code, which converts html name to their corresponding symbols.

Example :- €-> this is euro html name , &#8364 ->. this is decimal code for euro;

My code will convert this name to euro symbol-> €

But when I am converting &#8377 to ₹ , it not working.

My Console Application code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Multiple_Replace
{
    class Program
    {
        static void Main(string[] args)
        {
        var input = " ₹ 5000 €";
        var replacements = new Dictionary<string, string> { { "₹", "&#8377;" }, {"&euro;", "&#8364;"} };
        var output = replacements.Aggregate(input, (current, replacement) => current.Replace(replacement.Key, replacement.Value));
        Console.WriteLine(output);
        Console.ReadLine();
        }
    }
}

Please help me out.

Upvotes: 4

Views: 1458

Answers (2)

ib11
ib11

Reputation: 2568

First, I think there is a more basic issue here.

Your input string does not contain the string "&euro;".

If you change the Dictionary to this:

var replacements = new Dictionary<string, string> { { "₹", "&#8377;" }, { "€", "&#8364;" } };

Then you will see that the output is in fact:

&#8377; 5000 &#8364;

So you are not seeing what you think you see because while the "₹" is part of the string the "&euro;" is not.


That said, reading up on this, it seems that this html entity code for the Rupee symbol is not supported by all browsers.

The following code works using the unicode code if this is what you want to accomplish:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Multiple_Replace
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = " ₹ 5000 €";
            var replacements = new Dictionary<string, string> { { "₹", "\u20B9" }, { "€", "\u20AC" } };
            var output = replacements.Aggregate(input, (current, replacement) => current.Replace(replacement.Key, replacement.Value));
            Console.WriteLine(output);
            Console.ReadLine();
        }
    }
}

Also, check this question and the accepted answer, I think this will be informative for you:

Displaying the Indian currency symbol on a website

And finally if you want a correct representation of the symbol in an html, you can use this code:

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

<i class="fa fa-inr"></i>

HTH

Upvotes: 1

H77
H77

Reputation: 5967

Have you tried setting the console output encoding?

e.g.

Console.OutputEncoding = System.Text.Encoding.UTF8;

Upvotes: 0

Related Questions