In Form_load I have
txtAlteFonduri.Text = "5,00";
txtFReparatii.Text = "15,00";
txtFRulment.Text = "20,00";
and in another function I want to parse text as decimal
decimal alteFonduri = Decimal.Parse(txtAlteFonduri.Text);
decimal fondRulment = Decimal.Parse(txtFRulment.Text);
decimal fondRepar = Decimal.Parse(txtFReparatii.Text);
but I have an error in the second line
Input string was not in a correct format.
You need to specifically add the number format. For your examples above, the following should work:
decimal alteFonduri = Decimal.Parse(txtAlteFonduri.Text, CultureInfo.GetCulture("de-DE"));
Otherwise, the system's culture information is used.
You are using a different culture to what decimal.Parse() is expecting (it expects the decimal point '.' but you provide a comma. Using the correct culture should correctly parse the strings, although I can run your code without having any errors...
You can use Decimal.Parse(variable, CultureInfo.GetCultureInfo("Culture-Name"));
You have to use this overload of Decimal.Parse and supply a IFormatProvider matching the culture of your input. You should also consider using one of the Decimal.TryParse methods for better error handling.
Related
Please observe following simple code. Why the variable inVarient prints without decimal point. I want decimal point, how to achieve it?
decimal actualVal = 1247315.93m;
string inSwedish = actualVal.ToString(CultureInfo.CreateSpecificCulture("sv-SE"));
decimal inVarient = decimal.Parse(inSwedish, CultureInfo.InvariantCulture);
Console.WriteLine(inSwedish); //prints 1247315,93 (as intended)
Console.WriteLine(inVarient); //prints 124731593 (I need 1247315.93)
Console.Read();
This is because you're trying to parse a string that represents a decimal formatted with the Swedish culture but you're trying to parse it with the invariant culture, which won't treat a comma as a decimal point. You need something like this:
decimal actualVal = 1247315.93m;
var culture = CultureInfo.CreateSpecificCulture("sv-SE");
string inSwedish = actualVal.ToString(culture));
decimal invariant = decimal.Parse(inSwedish, culture);
Console.WriteLine(inSwedish);
Console.WriteLine(invariant.ToString(CultureInfo.InvariantCulture));
Console.Read();
Swedish culture uses , as a decimal separator, the invariant culture uses ., so when it parses a string using the comma as a separator it just ignores it.
The reason why your decimal point disappears here is because in the invariant culture, a comma is a thousands separator, not a decimal separator. Thus, it assumes it can safely ignore commas when parsing a numeric text.
If you go back and forth from a culture to another, this kind of thing is to be expected.
I don't know what your real context is but if you want to recover the numeric as it originally was before it was made a string, you must either use consistent formatters throughout your code or not storing decimals into string variables only to parse them back to decimal again.
If there's no need to serialize anything the latter is the way to go.
I need to write decimal value to ms access database, but i have a problem with conversion values to decimal in different cultures. Have a values from file, which separates by commma. I try:
public decimal CSingleCulture (string str)
{
string sep = System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
string s = str.Replace(",", sep);
return decimal.Parse(s);
}
if NumberDecimalSeparator = "." then work is good, but if NumberDecimalSeparator = "," problems begin... decimal.Parse(s) always return vlaues separates by dot. In this situation, when inserted into a database error occurs.
The recommended way to deal with this is to store the value as a number rather than a string. Both in the database and in your program. When you do that, your current problem simply never arises.
The only time you deal with numbers in string format is when you display them, or accept user input. In those scenarios you can use the user's culture settings to let them see and use their preferred separator.
Should you ever need to convert between string and number for persistence then you must use culture invariant conversion. This appears to be where you are falling down. I suspect that the file you read has no well-defined format. Make sure that when you read and write the file you use CultureInfo.InvariantCulture. If the file does have a well-defined format that differs from the invariant culture, then use an appropriate specific CultureInfo.
Can't actually understand what is it you're trying to accomplish, and I have to agree with the other answer. But one other thing that's good to know is you can use invariant culture like so:
double.Parse("15.0", CultureInfo.InvariantCulture)
This will always expect dot character to delimit your decimal digits regardless of what is set in current thread's culture.
I have several strings that I need to convert to float. When I try to do so, using System.Convert.ToSingle(MyString), I always get a FormatException.
I have tried even creating strings like "12.123", to make sure the numbers are okay, but again I got the exception. My question is, what is the correct format then? In what format should the number in string be?
Example of one of many strings I will convert: 50.105128
It could be down to your system's culture which may be set to using a , as the separator. Setting the format to InvariantCulture will use a . for the separator.
Convert.ToSingle("12.123", CultureInfo.InvariantCulture)
It depends on culture, but you can use invariant in this way:
Convert.ToSingle("0", System.Globalization.CultureInfo.InvariantCulture);
Convert.ToSingle is culture-sensitive. In culture installed on your machine, decimal separator might be different from comma, and number may look like 50,105128
Use this overload instead, which allows to specify culture:
public static float ToSingle(
Object value,
IFormatProvider provider
)
I have textbox that accept numbers. Those numbers will be saved in database.
When I enter number like 2,35 and convert to float and send to database I get error because database accept only number with dot, e.g. 2.35
float num = float.Parse(textBox1.Text);
num is still 2,25
How to manage that? I've tried with CultureInfo.InvariantCulture but I never get what I want
You can try the following:
float.Parse(textBox1.Text.Trim(), CultureInfo.InvariantCulture.NumberFormat);
I hope this would've solved the issue.
The easiest way is to replace ',' with '.' in like:
float num = float.Parse(textBox1.Text);
string stringValue = num.ToString().Replace(',', '.');
Then send "stringValue" to database.
I hope that helps you.
use this:
CultureInfo.CreateSpecificCulture("en-US");
I have same problem back then and it solved by code above
num is 2,25 because it's shown to you in your culture. It will be passed correctly to the database, provided you use the usual mechanisms (i.e. prepared statements with parameters). If you insist on manually gluing together SQL, then by all means use InvariantCulture to format the number, but generally, please don't.
This is a common globalization issue. What you have to define is a single culture in which to store the data itself, since you are storing it as a string value. Then, do ALL your data input and handling using that culture. In our code, we have several blocks that look similar to this in order to handle multi-cultural math and data display:
//save current culture and set to english
CultureInfo current = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
//Do Math and Data things
//restore original culture
System.Threading.Thread.CurrentThread.CurrentCulture = current;
This way you can make sure that all the data is handled and stored the same way, regardless of the culture being use to generate or display the data.
Edit
To do the data save, and converting the number to a string, you would do things exactly the same way. While you have the current threads CultureInfo setting as "en-US", the .ToString() methods from all the numbers will use "." instead of "," for the decimal point. The other way to do it is specify a format provider when calling .ToString().
decimalNumber.ToString(new CultureInfo("en-US"));
This specifies that when you convert the number to a string, use the NumberFormat from the provided culture.
Where can I set style in which double is writte by ToString() method ?
I am getting for example 2,2345 while I want to have 2.2345
thanks for ay hints,
bye
double a = 2.2345;
string b = a.ToString(CultureInfo.InvariantCulture);
You need to specify a FormatProvider, ususally as a CultureInfo.
For example:
string s = d1.ToString(System.Globalization.CultureInfo.InvariantCulture);
The Double.ToString() method is overloaded, so you can call it with a couple of different signatures to get the output you want.
In this case, your culture settings are affecting the output of the ToString() function. To get the result that you want, you should call Double.ToString(IFormatProvider), passing in CultureInfo.InvariantCulture:
myDouble.ToString(CultureInfo.InvariantCulture);
This should solve your issue.
For future reference, note that another common version of Double.ToString() is the Double.ToString(String) overload. The String parameter is a numeric format string, either one that is predefined or one that you specify. For example:
myDouble.ToString("format string here");
MSDN has a couple of articles on format strings: Standard Numeric Format Strings and Custom Numeric Format Strings.
If you would want the affect to be on your whole application. This could be set on initialization. Thus, note that this would mean an effective change on ALL cultures.
var culture = new CultureInfo(Thread.CurrentThread.CurrentCulture.Name);
culture.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = culture;
Then just:
string value = (1002.300).ToString();