How to get Current culture formatted number currency - c#

Using below way, able to get 2 digit currency for current culture.
string.Format(CultureInfo.CurrentCulture, "{0:C2}", amount)
getting the result as: $1,000.00
How can we get currency using currency symbol like USD or EUR as below,
expected result: 1,000.00 USD

This can be achieved using the RegionInfo class:
decimal amount = 1000m;
CultureInfo culture = CultureInfo.CurrentCulture;
RegionInfo region = RegionInfo.CurrentRegion; //or new RegionInfo(...)
string formattedCurrency = string.Format(CultureInfo.CurrentCulture, "{0:C2} {1}", amount, region.ISOCurrencySymbol);
In the above code, formattedCurrency results in a string value of "$1,000.00 USD".
If you're expecting no currency symbol, you could either do something like Replace(culture.NumberFormat.CurrencySymbol, "") or use the N specifier instead of C. Both result in a value of "1,000.00 USD" (with my region/culture).
This specific topic is addressed in this question: Format a double value like currency but without the currency sign (C#)
Note that the accepted answer (using N) appears to not be friendly across all cultures.

Related

System.FormatException if use new CultureInfo("id-ID") - Indonesian culture

I am trying to convert exponential value to decimal with Indonesian culture but it does not work..
string _ex = "1.2162876E-5";
decimal _dec = decimal.Parse(_ex, NumberStyles.Float, new CultureInfo("id-ID"));
it thrown me System.FormatException
but it works properly if I use en-US
decimal d0 = decimal.Parse(_ex, NumberStyles.Float, new CultureInfo("en-US"));
what I missed?
please help
thanks a lot
Don2
In id-ID, the decimal separator (CultureInfo.NumberFormat.NumberDecimalSeparator) is ",", so it is expecting "1,2162876E-5" as the input (meaning the numeric value zero point zero zero zero zero one two etc). If the input is using a "." to mean decimal point, then you should probably use the invariant culture to parse it (CultureInfo.InvariantCulture), instead of a locale-specific culture.

Converting string with many leading zeros to decimal

How can I convert string which looks like this:
0005.47
to decimal value 5.47.
This value is kept in newStringArray[1] so I did this: Convert.ToDecimal(newStringArray[1])
But as a result I got this value : 547
What's the point here?
Your culture settings think that a point is not the decimal separator but the thousands separator.
You need to pass the information about the culture you want to use to Convert.ToDecimal. It can be done passing the CultureInfo.InvariantCulture property to inform the converter to use the proper decimal symbol when converting.
string test = "0005.47";
decimal value = Convert.ToDecimal(test, CultureInfo.InvariantCulture);

Currency format from currency symbol

I do have values and currency symbols in my database (BRL, USD and etc) and would like use that to write a formatted string, something like:
int number = 100;
string currencySymbol = "USD";
string formattedNumber = number.ToString("C", currencySymbol);
I've tried to cast currencySymbol to cultureInfo but it is not possible as written in this post get CultureInfo from a Currency Code?
I believe you are looking to use custom currency symbols in formatting. You can do:
int number = 100;
var numberFormatInfo = (NumberFormatInfo) NumberFormatInfo.CurrentInfo.Clone();
numberFormatInfo.CurrencySymbol = "USD";
string formattedNumber = number.ToString("C", numberFormatInfo);
A better approach would be to store different culture information ("en-US") in database and then retrieve the culture based on it and use its predefined currency symbol. But it will not be same as yours. In case of US, it is $ and not USD.

Formatting a decimal value with ToString() to have commas as thousand separators where the amount of decimal places is unknown

I have many decimals, each rounded differently:
decimal quantity = Decimal.Round(item.Quantity.Value, 2,
MidpointRounding.AwayFromZero);
decimal difference = Decimal.Round(quantity * eva, 0,
MidpointRounding.AwayFromZero);
When binding to the UI, I convert to string like this:
string Quantity = quantity.ToString("G", CultureInfo.InvariantCulture);
string Difference = difference.ToString("G", CultureInfo.InvariantCulture);
Is there a generic way to insert commas for thousand separators while keeping the original decimal rounding the same?
Try using Format.
double d = 1.234567;
String output = d.ToString("#,##0.##");
Also,
double d = 123456789.1;
string format = d.ToString().
IndexOf(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
>=0 ? "#,##0.00" : "#,##0";
Console.WriteLine (d.ToString(format));
For anyone wondering, I ended up using String.Format(new CultureInfo("en-US"), "{0:N}", difference) and changed the N depending on how many decimal places I needed.
You can use the "N" format specifier and supply the number of digits you want any number to retain. If you want each number to potentially have a different number of digits you wall have to determine the number to supply to the format string each time.
quantity.ToString("N(digits)");
Complete documentation is at http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#NFormatString

How do I convert a string to a decimal, and format it for pretty output?

I want to convert "372551.40" to decimal. But I need to see it after converting this format 372.551,40.
To convert it to decimal, you can use:
decimal decimalValue = 0.0;
decimalValue = decimal.Parse("372551.40");
or
decimal.TryParse("372551.40", out decimalValue);
To display it in a specific format you can do:
CultureInfo tr = new CultureInfo("tr-TR");
string formattedValue = decimalValue.ToString("c", tr);
//result will be 372.551,40 YTL
formattedValue = decimalValue.ToString("0,0.00", tr);
//result will be 372.551,40
string value;
Decimal number;
value = "16,523,421";
if (!Decimal.TryParse(value,out number))
{
// set it to something if the "Value" is not a number
number = -1;
}
Do the following:
string s = "372551.40";
CultureInfo cultureInfo = CultureInfo.InvariantCulure; //Use relevant culture in which your number is formatted. In this case InvariantCulture would do.
decimal d;
bool succesful = Decimal.TryParse(s, NumberStyles.Number, cultureInfo, out d); //it will try to parse the string according to the specified culture.;
If you have a succesful parse, then d will store the numeric value represented by s as a decimal value which you can output into any formatted string and culture the ToString() or Format.String().
Note that if the culture in which the number represented by s is the current system culture, then you can use the TryParse(string s, out decimal d) overload where it is not necessary to specify NumberStyles and IFormatProvider.
Something like this?
string s = "372551.40";
decimal d;
if (decimal.TryParse(s, out d))
{
var culture = new CultureInfo("de-DE");
var result = d.ToString("0,0.00", culture);
// result is "372.551,40"
}
You can also use the current culture instead of hard-coding one like I did.
Hope this helps,
John
Use decimal.Parse() to make it a decimal. Then you have many formatting options.
The display as you mentioned is dependent on the culture setting.
Make your new CultureInfo and in the NumberFormat, you will have to modify some settings like Decimal Separator as , and Thousands Separator as . and provide this to the ToString method of the variable holding the decimal value.
This should display the value as 372.551,40
You can use .Replace
string string 1 = "372,551.40";
string1.Replace(",","");
decimalVal = System.Convert.ToDecimal(StringVal);
//shows 372551.40
You can always throw that into a for loop if you are playign with a ton of numbers.
You can find more in depth info and some examples on MSDN
The overload of decimal.Parse that takes an IFormatProvider will allow you to parse strings containing numbers with periods as decimal point symbols (in case the standard is a comma in your culture).
You can use ToString on the resulting decimal to format it with a comma by passing in an appropriate IFormatProvider. Both CulturInfo and NumberFormatInfo implement IFormatProvider.
You can get an instance of CultureInfo with the following code (this one is for English in Australia).
new CultureInfo("en-AU")
Also note that decimal.TryParse is a good alternative to the decimal.Parse method if you expect incorrectly formatted strings as it will allow you to handle them without an exception being raised.
The following code should give you the desired result (you wrote in one of the comments that the target system is SAP and that the culture is probably German (de-DE)).
var yourString = "372551.40";
var yourDecimal = decimal.Parse(yourString, CultureInfo.InvariantCulture);
var yourFormattedDecimal = yourDecimal.ToString(new CultureInfo("de-DE"));
From MSDN:
string value;
decimal number;
// Parse an integer with thousands separators.
value = "16,523,421";
number = Decimal.Parse(value);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// 16,523,421' converted to 16523421.
Cheers
You can create custom NumberFormatInfo:
string s = "372551.40";
var dec = decimal.Parse(s, CultureInfo.InvariantCulture);
var nfi = new CultureInfo("en-US", false).NumberFormat;
nfi.NumberGroupSeparator = ".";
nfi.NumberDecimalSeparator = ",";
var res = dec.ToString("n", nfi);
var resDecimal = decimal.Parse(res, nfi);
Output is exactly what you need: 372.551,40

Categories

Resources