Sai Nagarajan M M
Sai Nagarajan M M

Reputation: 45

How does convert.ToString(C0) behave?

I have different scenarios. I need output where the value must return comma separated values in ₹ format which it does in my system where I have the ₹ rupee symbol. Whereas in the user system C0 returns $ value with comma separated values I do not know if he has ₹ symbol in his system or not. Can anyone advise.

PS. I have use the expression in the subject line where I cannot use more functions. I have used convert.ToString("C0").

Upvotes: 3

Views: 3892

Answers (2)

P Walker
P Walker

Reputation: 592

You can always set the thread's current UI culture somewhere in your application, then use it when you need to output the correct currency symbol. For example:

double amount = 101.12;
Thread.CurrentThread.CurrentUICulture = new CultureInfo("hi-IN");
Console.WriteLine(amount.ToString("C0", Thread.CurrentThread.CurrentUICulture));

If the issue happens to be a question about whether the culture exists on the running computer, this code can help:

bool cultureExists = false;
try
{
    CultureInfo cultureInfo = new CultureInfo("hi-IN");
    cultureExists = true;
}
catch
{
    // nothing here
}

If you find that it doesn't exist, you'd have to then create it (assuming you have permissions on the machine sufficient for creating the culture). Here's a link that may help with this, if you need it: http://www.codeproject.com/Tips/988807/Net-custom-Culture-with-use-case

Upvotes: 1

Ricky Gummadi
Ricky Gummadi

Reputation: 5240

You can brute force search the string for any currency symbols and change them to whatever character you want eg:

string s = "$";
foreach (var c in s)
{
    var category = CharUnicodeInfo.GetUnicodeCategory(c);

    if (category == UnicodeCategory.CurrencySymbol)
    {
        //Force convert the char to what every character you want
    }
}

Upvotes: 1

Related Questions