In Germany the number 9.81 (US) is represented as
9,81
What's wrong with the following conversion?
CultureInfo culture = new CultureInfo("de-DE");
NumberStyles style = NumberStyles.Float ;//| NumberStyles.AllowThousands;
string str = "9,81";
double value = 0.0;
if (double.TryParse(str, style, culture, out value))
Console.WriteLine("Result of conversion: " + value);
else
Console.WriteLine("Numeric conversion failed!");
Numeric conversion failed!
BTW, thousands separator ('.') doesn't work either...
I need thousand and decimal separator as per current culture. If we are using US culture then for decimal it should '.' and for thousand it should ','. But for German culture default decimal separator is ',' and thousand separator is '.'.
for getting date time separator I am using following code
CultureInfo us = new CultureInfo("en-US");
string shortUsDateFormatString = us.DateTimeFormat.ShortDatePattern;
string dateseparator= us.DateTimeFormat.DateSeparator;
string timeseparator= us.DateTimeFormat.TimeSeparator;
Is there any way something same like date / time ?
The NumberFormat property has this info for both Currency and Numbers.
You can use the NumberFormat property of the CultureInfo which includes properties for the decimal separator and thousands separator
var numberFormat = us.NumberFormat;
string decimalSeparator = numberFormat.NumberDecimalSeparator;
string thousandsSeparator = numberFormat.NumberGroupSeparator;
By this code can get the current system settings :
System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
FORMAT_NUMBER_DECIMALSEPARATOR = ci.NumberFormat.CurrencyDecimalSeparator;
FORMAT_NUMBER_DIGITGROUP = ci.NumberFormat.CurrencyGroupSeparator;
I am having some problems converting string to decimal values with decimal.parse.
This is the line of code I have:
fixPrice = decimal.Parse(mItemParts.Groups["price"].Value.Replace("$", "").Replace(" ", "").Replace("usd", ""));
The value from which I am trying to convert is: '$779.99'
Then once the parsing to decimal happens, I am getting this value: 77999.
I would like to get 779.99 instead of 77999.
Thanks in advance, Laziale
Regex included: "#"\[^\""]+?)\""[^~]+?\]+?src=\""(?[^\""]+?)\""[^>]+?title=\""(?[^\""]+?)\""[^~]+?price\"">(?[^\<]+?)\<[^~]+?\(?[^\<]+?)\
I would use Decimal.TryParse():
decimal parsedDecimal = 0;
string yourCurrency = "$779.99";
bool didParse = Decimal.TryParse(yourCurrency,
NumberStyles.Currency,
new CultureInfo("en-US"), out parsedDecimal);
if(didParse) {
// Parse succeeded
}
else {
// Parse failed
}
It appears that you are running this in a culture where '.' is the group separator, and ',' is the decimal separator. To get around that, use the Parse overload that takes a CultureInfo:
fixPrice = decimal.Parse(stringExpression, CultureInfo.InvariantCulture);
Also look into the NumberStyles enum so you don't have to worry about currency signs yourself:
fixPrice = decimal.Parse(stringExpression, NumberStyles.Currency, new CultureInfo("en-US"));
Pass a CultureInfo instance of the culture you are parsing from.
CultureInfo inherits from IFormatProvider
edit:
Here is a sample for the conversion
Decimal.Parse(yourValue, NumberStyles.AllowCurrencySymbol |
NumberStyles.AllowDecimalPoint |
NumberStyles.AllowThousands,
CultureInfo.InvariantCulture);
This works for me:
string decStr = "$779.99";
CultureInfo ci = new CultureInfo("en-US");
decimal fixPrice = decimal.Parse(decStr, NumberStyles.Currency, ci);
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
I need to convert a decimal to currency string so i did this:
CultureInfo usa = new CultureInfo("en-US");
NumberFormatInfo nfi = usa.NumberFormat;
nfi.CurrencyDecimalDigits = 0;
myValueFormated = String.Format(nfi, "{0:C}", value);
It removed decimal places, gave me a comma separator for thousands and and currency symbol.
But I also need to display that number in thousands, rounded.
Any ideas?
Thanks
You need to do the rounding bit yourself:
value = Math.Round(value / 1000);