Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need currency to be only in the following string format: USD EUR etc.
I was thinking of using ISOCurrencySymbol:

but not sure how to exactly use it.

What I have tried:

public string Currency { get; private set; }

public string SetPrice(double price, string currency)
        {

            this.Price = price;
            if ( // Here I was thinking to add code so that the required condition which I described above is true )
            {
                this.Currency = currency;
            }
            return string.Format("Price = {0}, Currency = {1}", price, currency.);
        }
Posted
Updated 3-Jan-22 3:55am
v3

That was interesting! Try this:

using System;
using System.Globalization;
using System.Linq;

namespace ConsoleApp {
	internal class Program {
		static void Main(string[] args) {
			Console.WriteLine(GetPrice(123.43, "CAD"));
			Console.WriteLine(GetPrice(123.45, "JPY"));
			Console.WriteLine(GetPrice(123.47, "USD"));
			Console.WriteLine(GetPrice(123.49, "XYZ"));
			Console.ReadLine();
		}
		public static string GetPrice(double price, string currency) {
			RegionInfo region = CultureInfo
				.GetCultures(CultureTypes.SpecificCultures)
				.Select(ct => new RegionInfo(ct.LCID))
				.Where(ri=>ri.ISOCurrencySymbol==currency).FirstOrDefault();
			if (region != null) {
				return string.Format("{0} {1}", price, region.CurrencySymbol);
			} else {
				return string.Format("{0} {1} (unknown currency!)",
					price, currency);
			}
		}
	}
}



Notes:
- it only works for the currencies known to Windows (currently 100+), which may depend on your Windows version.
- it does not work for cryptocurrencies.
- many cultures expect their currency symbol in front of the number, I don't have a general solution for this.

:)
 
Share this answer
 
Comments
LuckyChloe 3-Jan-22 10:30am    
Thank You.
If in doubt, read the documentation: RegionInfo.ISOCurrencySymbol Property (System.Globalization) | Microsoft Docs[^] it includes a basic (but comprehensive) example code for usage.
 
Share this answer
 
Comments
LuckyChloe 3-Jan-22 6:18am    
"Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid."
OriginalGriff 3-Jan-22 6:37am    
'A few simple rules when posting your question.
Be courteous. Everyone here helps because they enjoy helping, not because it's their job.
Have you searched or Googled for a solution?
Be specific! eg "How do I change the dialog colour?" instead of "My code doesn't work. Help?"'

Since what you posted was "I dunno how to use it" pointing you to the documentation saved you the bother of googling ... and was about the only answer that could be given to your vague "question" ...

And since your question history leans very heavily to the "do it for me" school, that is what I did.
LuckyChloe 3-Jan-22 6:43am    
"If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome."
LuckyChloe 3-Jan-22 6:46am    
Ah just checked your profile. It says CEO. Maybe these rules are written by you. My apologizes. Still I would appreciate if anyone could help me solve this problem. I only asked 1 question before on the website and received really useful answers.
The thing that confusing us is how do you get RegionInfo? Because currency is tagged with the Region.

To make you understand, see below code which uses current regionInfo on runtime.

Console.WriteLine("Price = {0}, Currency = {1}",10.5,RegionInfo.CurrentRegion.ISOCurrencySymbol);
 
Share this answer
 
Comments
LuckyChloe 3-Jan-22 8:32am    
Sry I meant to write using ISOCurrencySymbol: (without RegionInfo)
_Asif_ 3-Jan-22 8:46am    
Can you share a sample call to SetPrice? This would help us understand what value are you passing in Currency parameter?
LuckyChloe 3-Jan-22 8:56am    
This is the task I am trying to complete - https://ibb.co/58stjvz. The wording is so confusing that I have been trying to complete this task whole day and still could not figure out what they expect from me. I see what you mean Your code example: Console.WriteLine("Price = {0}, Currency = {1}",10.5,RegionInfo.CurrentRegion.ISOCurrencySymbol); will always return USD since my region is set to US region. Which is not what we want.
Luc Pattyn 3-Jan-22 9:56am    
Why do you accept a solution that does not produce the result you want?
LuckyChloe 3-Jan-22 10:33am    
You are right. I should not have done that. I just wanted to somehow appreciate someone's effort for helping me out :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900