I have a long string with double-type values separated by # -value1#value2#value3# etc
I splitted it to string table. Then, I want to convert every single element from this table to double type and I get an error. What is wrong with type-conversion here?
string a = "52.8725945#18.69872650000002#50.9028073#14.971600200000012#51.260062#15.5859949000000662452.23862099999999#19.372202799999250800000045#51.7808372#19.474096499999973#";
string[] someArray = a.Split(new char[] { '#' });
for (int i = 0; i < someArray.Length; i++)
{
Console.WriteLine(someArray[i]); // correct value
Convert.ToDouble(someArray[i]); // error
}
There are 3 problems.
1) Incorrect decimal separator
Different cultures use different decimal separators (namely , and .).
If you replace . with , it should work as expected:
Console.WriteLine(Convert.ToDouble("52,8725945"));
You can parse your doubles using overloaded method which takes culture as a second parameter. In this case you can use InvariantCulture (What is the invariant culture) e.g. using double.Parse:
double.Parse("52.8725945", System.Globalization.CultureInfo.InvariantCulture);
You should also take a look at double.TryParse, you can use it with many options and it is especially useful to check wheter or not your string is a valid double.
2) You have an incorrect double
One of your values is incorrect, because it contains two dots:
15.5859949000000662452.23862099999999
3) Your array has an empty value at the end, which is an incorrect double
You can use overloaded Split which removes empty values:
string[] someArray = a.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
Add a class as Public and use it very easily like convertToInt32()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Common
/// </summary>
public static class Common
{
public static double ConvertToDouble(string Value) {
if (Value == null) {
return 0;
}
else {
double OutVal;
double.TryParse(Value, out OutVal);
if (double.IsNaN(OutVal) || double.IsInfinity(OutVal)) {
return 0;
}
return OutVal;
}
}
}
Then Call The Function
double DirectExpense = Common.ConvertToDouble(dr["DrAmount"].ToString());
Most people already tried to answer your questions.
If you are still debugging, have you thought about using:
Double.TryParse(String, Double);
This will help you in determining what is wrong in each of the string first before you do the actual parsing.
If you have a culture-related problem, you might consider using:
Double.TryParse(String, NumberStyles, IFormatProvider, Double);
This http://msdn.microsoft.com/en-us/library/system.double.tryparse.aspx has a really good example on how to use them.
If you need a long, Int64.TryParse is also available: http://msdn.microsoft.com/en-us/library/system.int64.tryparse.aspx
Hope that helps.
private double ConvertToDouble(string s)
{
char systemSeparator = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator[0];
double result = 0;
try
{
if (s != null)
if (!s.Contains(","))
result = double.Parse(s, CultureInfo.InvariantCulture);
else
result = Convert.ToDouble(s.Replace(".", systemSeparator.ToString()).Replace(",", systemSeparator.ToString()));
}
catch (Exception e)
{
try
{
result = Convert.ToDouble(s);
}
catch
{
try
{
result = Convert.ToDouble(s.Replace(",", ";").Replace(".", ",").Replace(";", "."));
}
catch {
throw new Exception("Wrong string-to-double format");
}
}
}
return result;
}
and successfully passed tests are:
Debug.Assert(ConvertToDouble("1.000.007") == 1000007.00);
Debug.Assert(ConvertToDouble("1.000.007,00") == 1000007.00);
Debug.Assert(ConvertToDouble("1.000,07") == 1000.07);
Debug.Assert(ConvertToDouble("1,000,007") == 1000007.00);
Debug.Assert(ConvertToDouble("1,000,000.07") == 1000000.07);
Debug.Assert(ConvertToDouble("1,007") == 1.007);
Debug.Assert(ConvertToDouble("1.07") == 1.07);
Debug.Assert(ConvertToDouble("1.007") == 1007.00);
Debug.Assert(ConvertToDouble("1.000.007E-08") == 0.07);
Debug.Assert(ConvertToDouble("1,000,007E-08") == 0.07);
In your string I see: 15.5859949000000662452.23862099999999 which is not a double (it has two decimal points). Perhaps it's just a legitimate input error?
You may also want to figure out if your last String will be empty, and account for that situation.
Related
Now i have this problem here with comma and point in decimal point and thousands separator.
My program gets prices from different sources .
some american some european
Some prices comes like this 2000,0.20 for 20000.20
and some like 2000.0,20 for again 20000.20
I couldn't find a way to make my code to recognize these two formats .
i tried to use replace to turn the comma to dot but if there is thousands separator in the number i get problems. how can i convert string to double without these kind of problems ?
I tried this
but its just dont work if there is two different culture
double.TryParse(price, NumberStyles.Currency, CultureInfo.InvariantCulture, out priceD);
This is what I came up with
double FixUnknownCurrency(string amountText)
{
amountText = amountText?.Trim()?.Replace(" ", string.Empty);
if(string.IsNullOrWhiteSpace(amountText))
return 0d;
if(amountText.Length < 3)
return double.Parse(amountText);
var currencyDecimal = amountText[amountText.Length-3];
if(Char.IsNumber(currencyDecimal))
return double.Parse(amountText);
if(currencyDecimal == '.' || currencyDecimal == ',')
{
amountText = amountText.Replace(",", string.Empty).Replace(".", string.Empty);
return double.Parse(amountText) / 100d;
}
return double.Parse(amountText);
}
Try it out here. Running late for train: https://dotnetfiddle.net/HTiL4s
I'm not aware of a better way other than trying different cultures to see what succeeds.
Create a method like this:
public double CurrencyToDouble(string input)
{
// Will hold the result
double result = 0;
// Try US culture
if(double.TryParse(input, System.Globalization.NumberStyles.Currency, System.Globalization.CultureInfo.GetCultureInfo("en-US"), out result)) { return result; }
// Try DE culture
if (double.TryParse(input, System.Globalization.NumberStyles.Currency, System.Globalization.CultureInfo.GetCultureInfo("de-DE"), out result)) { return result; }
// Try current system culture
if(double.TryParse(input, System.Globalization.NumberStyles.Currency, System.Globalization.CultureInfo.CurrentCulture, out result)) { return result; }
// Try invariant culture
if(double.TryParse(input, System.Globalization.NumberStyles.Currency, System.Globalization.CultureInfo.InvariantCulture, out result)) { return result; }
// Try more common cultures...
// Unsuccessful, throw some kind of error
throw new FormatException("Could not convert!");
}
Input:
CurrencyToDouble("2000,0.20");
CurrencyToDouble("2000.0,20");
Output:
20000.2
20000.2
I have a textbox that allows users to input a decimal values that then gets stored in the a table in the database, this piece of code works in the development environment. I have now published the my project to the server and now is not longer taking the values with the decimal places.
decimal ReceiptAmount;
decimal AmountDue;
decimal Change;
if (!string.IsNullOrEmpty(((TextBox)dl_Item.FindControl("tb_ReceiptAmount")).Text))
{
if (((TextBox)dl_Item.FindControl("tb_ReceiptAmount")).Text.Contains(".") == true)
{
ReceiptAmount = Convert.ToDecimal(((TextBox)dl_Item.FindControl("tb_ReceiptAmount")).Text.Replace(".", ","));
}
else
{
ReceiptAmount = Convert.ToDecimal(((TextBox)dl_Item.FindControl("tb_ReceiptAmount")).Text);
}
}
else
{
ReceiptAmount = 0;
}
if (!string.IsNullOrEmpty(((TextBox)dl_Item.FindControl("tb_AmountDue")).Text))
{
if (((TextBox)dl_Item.FindControl("tb_AmountDue")).Text.Contains(".") == true)
{
AmountDue = Convert.ToDecimal(((TextBox)dl_Item.FindControl("tb_AmountDue")).Text.Replace(".", ","));
}
else
{
AmountDue = Convert.ToDecimal(((TextBox)dl_Item.FindControl("tb_AmountDue")).Text);
}
}
else
{
AmountDue = 0;
}
if (!string.IsNullOrEmpty(((TextBox)dl_Item.FindControl("tb_Change")).Text))
{
if (((TextBox)dl_Item.FindControl("tb_Change")).Text.Contains(".") == true)
{
Change = Convert.ToDecimal(((TextBox)dl_Item.FindControl("tb_Change")).Text.Replace(".", ","));
}
else
{
Change = Convert.ToDecimal(((TextBox)dl_Item.FindControl("tb_Change")).Text);
}
}
else
{
Change = 0;
}
I am not to sure what seems to be the problem with this piece of code. The Textbox are found in a datalist that I loop through to get all of the values.
The Convert.ToDecimal overload that takes a string as input will parse the string using the CultureInfo.CurrentCulture. Probably your server has different regional settings. Depending on regional settings, a comma or point may be either interpreted as a thousand separator (and thus ignored) or as the decimal separator.
Instead, you should use Decimal.Parse directly, providing either a specific culture or the invariant culture, depending on your use case.
Ideally, you'd set the culture of the user somewhere. To achieve this there are multiple approaches, e.g. for ASP.Net Web forms: https://msdn.microsoft.com/en-us/library/bz9tc508.aspx
If you parse the string using the correct culture, you can get rid of the string manipulation for replacing . with ,.
First of all, lines like
if (!string.IsNullOrEmpty(((TextBox)dl_Item.FindControl("tb_ReceiptAmount")).Text))
look very ugly; let's extract a method (copy/paste is very, very bad practice):
private String FindDLText(String controlName) {
var box = dl_Item.FindControl(controlName) as TextBox;
return box == null ? null : box.Text;
}
Then you don't need checking Text.Contains(".") == true, just Replace if you really need it:
private Decimal FindDLValue(String controlName) {
String text = FindDLText(controlName);
if (String.IsNullOrEmpty(text))
return 0.0M;
//TODO: check if you really need this
//text = text.Replace(".", ",");
// you have to specify Culture either InvariantCulture or some predefined one;
// say, new CultureInfo("ru-RU") // <- use Russian Culture to parse this
return Decimal.Parse(text, CultureInfo.InvariantCulture);
}
Finally, you can get
decimal ReceiptAmount = FindDLValue("tb_ReceiptAmount");
decimal AmountDue = FindDLValue("tb_AmountDue");
decimal Change = FindDLValue("tb_Change");
feel the difference: three evident lines and two simple methods.
Objective
Sort a string that is displaying currency data like this $1,995.94 numerically in a set of data.
Code
I'm currently using the below code sample to convert the string value to decimal so that I can sort it properly.
if (sortBy == "checkAmount")
{
StringBuilder sb = new StringBuilder();
foreach (var c in Convert.ToString(p.GetType().GetProperty(sortBy).GetValue(p, null)))
{
if (!char.IsDigit(c) && c != '.') { continue; }
sb.Append(c);
}
return Convert.ToDecimal(sb.ToString());
}
else
{
return p.GetType().GetProperty(sortBy).GetValue(p, null);
}
Problem
What's a better way of doing this? It works, and that's cool, but it's not very elegant.
Final Solution
The answer provided by Servy works as expected, and I used that implementation for a while, but a colleague and I found an even better way so I'm documenting it here. BTW, I ended up using this solution in the end.
decimal.Parse(input, NumberStyles.AllowCurrencySymbol | NumberStyles.Number);
How about this, but only works for one string value. So you need to get your string split by $ and then do the conversion while saving into the array or list
using System.Globalization;
//rest of your code
string str = "$50,550.20";
decimal decval;
bool convt = decimal.TryParse(str, NumberStyles.Currency,
CultureInfo.CurrentCulture.NumberFormat, out decval);
if (convt)
Console.WriteLine(decval);
Console.ReadLine();
Here is a simpler solution:
public static decimal ToDecimal(this string str)
{
return decimal.Parse(str, NumberStyles.Currency);
}
and the unit test:
[Test]
public void ToDecimal_Convert_String_To_Decimal()
{
Assert.AreEqual(1234M, "1234".ToDecimal());
Assert.AreEqual(-1234.56M, "$(1,234.56)".ToDecimal());
Assert.AreEqual(1234.56M, "$1,234.56".ToDecimal());
}
Here is a method that most closely resembles the code you've provided
public static decimal Parse(string input)
{
return decimal.Parse(Regex.Replace(input, #"[^\d.]", ""));
}
Here is an option that will support negative numbers, and will stop if it finds a second period value, thus reducing the number of strings it returns that are not valid decimal values. It also has a few other modifications not seen in the OP to handle additional cases your current code doesn't.
public static decimal Parse(string input)
{
return decimal.Parse(Regex.Match(input, #"-?\d{1,3}(,\d{3})*(\.\d+)?").Value);
}
decimal amount = decimal.Parse("$123,456.78",
NumberStyles.AllowCurrencySymbol |
NumberStyles.AllowThousands |
NumberStyles.AllowDecimalPoint);
works for all culture:
var d = decimal.Parse("$497.7", NumberStyles.Currency, CultureInfo.CreateSpecificCulture("us-US").NumberFormat);
Console.WriteLine(d);
public static decimal ToDecimalFromStringDecimalOrMoneyFormattedDecimal(this string s)
{
try
{
return decimal.Parse(s);
}
catch
{
var numberWithoutMoneyFormatting = Regex.Replace(s, #"[^\d.-]", "");
return decimal.Parse(numberWithoutMoneyFormatting);
}
}
[Test]
public void Test_ToDecimalFromStringDecimalOrMoneyFormattedDecimal()
{
Assert.That("$ 500".ToDecimalFromStringDecimalOrMoneyFormattedDecimal() == (decimal)500);
Assert.That("R -500".ToDecimalFromStringDecimalOrMoneyFormattedDecimal() == (decimal)-500);
Assert.That("-$ 500".ToDecimalFromStringDecimalOrMoneyFormattedDecimal() == (decimal)-500);
Assert.That("P 500.90".ToDecimalFromStringDecimalOrMoneyFormattedDecimal() == (decimal)500.9);
Assert.That("$ -50 0,090,08.08".ToDecimalFromStringDecimalOrMoneyFormattedDecimal() == (decimal)-50009008.08);
}
We can try to use decimal.TryParse to cast the currency string value to a decimal number.
NumberStyles:use NumberStyles.Currency which indicates that all styles except AllowExponent and AllowHexSpecifier are used. This is a composite number style.
CultureInfo: we need to set the right culture of currency which align with your currency value.
For this example, $20,000.00 is USA currency we can try to use English (USA) for that, on the other hand 20,000.00€ is euro currency we can try to use English (Ireland) that uses the euro currency.
decimal.TryParse("20,000.00€", NumberStyles.Currency ,CultureInfo.CreateSpecificCulture("en-IE"), out var euro);
Console.WriteLine(euro);
decimal.TryParse("$20,000.00", NumberStyles.Currency ,CultureInfo.CreateSpecificCulture("en-us"), out var dollar);
Console.WriteLine(dollar);
c# online
I'd like to know if I'm missing something or not... I'm running under the standard Great British culture.
Double result = 0;
if (Double.TryParse("1,2,3", NumberStyles.Any, CultureInfo.CurrentCulture, out result))
{
Console.WriteLine(result);
}
Expected output would be nothing... "1,2,3" shouldn't parse as a double. However it does. According to the .NET 2.0 MSDN documentation
AllowThousands Indicates that the numeric string can have group
separators; for example, separating the hundreds from the thousands.
Valid group separator characters are determined by the
NumberGroupSeparator and CurrencyGroupSeparator properties of
NumberFormatInfo and the number of digits in each group is determined
by the NumberGroupSizes and CurrencyGroupSizes properties of
NumberFormatInfo.
Allow thousands is included in NumberStyles.Any. The NumberGroupSizes is 3 for my culture. Is this just a bug in the Double.Parse? seems unlikely but I can't spot what I'm doing wrong....
It just means the input string can contain zero or more instances of NumberFormatInfo.NumberGroupSeparator. This separator can be used to separate groups of numbers of any size; not just thousands. NumberFormatInfo.NumberGroupSeparator and NumberFormatInfo.NumberGroupSizes are used when formatting decimals as strings. Using Reflector it seems like NumberGroupSeparator is only used to determine if the character is a separator, and if it is, it is skipped. NumberGroupSizes is not used at all.
If you want to validate the string, you could do so using RegEx or write a method to do so. Here's one I just hacked together:
string number = "102,000,000.80";
var parts = number.Split(',');
for (int i = 0; i < parts.Length; i++)
{
var len = parts[i].Length;
if ((len != 3) && (i == parts.Length - 1) && (parts[i].IndexOf('.') != 3))
{
Console.WriteLine("error");
}
else
{
Console.WriteLine(parts[i]);
}
}
// Respecting Culture
static Boolean CheckThousands(String value)
{
String[] parts = value.Split(new string[] { CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator }, StringSplitOptions.None);
foreach (String part in parts)
{
int length = part.Length;
if (CultureInfo.CurrentCulture.NumberFormat.NumberGroupSizes.Contains(length) == false)
{
return false;
}
}
return true;
}
Usually when I have need to convert currency string (like 1200,55 zł or $1,249) to decimal value I do it like this:
if (currencyString.Contains("zł)) {
decimal value = Decimal.Parse(dataToCheck.Trim(), NumberStyles.Number | NumberStyles.AllowCurrencySymbol);
}
Is there a way to check if string is currency without checking for specific currency?
If you just do the conversion (you should add | NumberStyles.AllowThousands
| NumberStyles.AllowDecimalPoint as well) then if the string contains the wrong currency symbol for the current UI the parse will fail - in this case by raising an exception. It it contains no currency symbol the parse will still work.
You can therefore use TryParse to allow for this and test for failure.
If your input can be any currency you can use this version of TryParse that takes a IFormatProvider as argument with which you can specify the culture-specific parsing information about the string. So if the parse fails for the default UI culture you can loop round each of your supported cultures trying again. When you find the one that works you've got both your number and the type of currency it is (Zloty, US Dollar, Euro, Rouble etc.)
As I understand it's better to do:
decimal value = -1;
if (Decimal.TryParse(dataToCheck.Trim(), NumberStyles.Number |
NumberStyles.AllowCurrencySymbol,currentCulture, out value)
{do something}
See Jeff Atwood description about TryParse. It doesn't throw an exception and extremely faster than Parse in exception cases.
To check if a string is a currency amount that would be used for entering wages - I used this:
public bool TestIfWages(string wages)
{
Regex regex = new Regex(#"^\d*\.?\d?\d?$");
bool y = regex.IsMatch(wages);
return y;
}
You might try searching the string for what you think is a currency symbol, then looking it up in a dictionary to see if it really is a currency symbol. I would just look at the beginning of the string and the end of the string and pick out anything that's not a digit, then that's what you look up. (If there's stuff at both ends then I think you can assume it's not a currency.)
The advantage to this approach is that you only have to scan the string once, and you don't have to test separately for each currency.
Here's an example of what I had in mind, although it could probably use some refinement:
class Program
{
private static ISet<string> _currencySymbols = new HashSet<string>() { "$", "zł", "€", "£" };
private static bool StringIsCurrency(string str)
{
// Scan the beginning of the string until you get to the first digit
for (int i = 0; i < str.Length; i++)
{
if (char.IsDigit(str[i]))
{
if (i == 0)
{
break;
}
else
{
return StringIsCurrencySymbol(str.Substring(0, i).TrimEnd());
}
}
}
// Scan the end of the string until you get to the last digit
for (int i = 0, pos = str.Length - 1; i < str.Length; i++, pos--)
{
if (char.IsDigit(str[pos]))
{
if (i == 0)
{
break;
}
else
{
return StringIsCurrencySymbol(str.Substring(pos + 1, str.Length - pos - 1).TrimStart());
}
}
}
// No currency symbol found
return false;
}
private static bool StringIsCurrencySymbol(string symbol)
{
return _currencySymbols.Contains(symbol);
}
static void Main(string[] args)
{
Test("$1000.00");
Test("500 zł");
Test("987");
Test("book");
Test("20 €");
Test("99£");
}
private static void Test(string testString)
{
Console.WriteLine(testString + ": " + StringIsCurrency(testString));
}
}