My website extracts data from a text-file into a database. After reading the data, I convert string-values into double, using:
oracom.Parameters.Add("nstpreisvm", OracleDbType.Double).Value = Convert.ToDouble(_material.Mat_StPreisVm);
If I check the results in the database now, (The column expects the NUMBER-format), I get two different values: On my localhost via Visual Studio 2010, the database returns 10,15 - But if I make the process on the webserver, the database only shows 10. How does this happen?
I implemented a little lable on the website now, filling it with the current culture on the Page-Load:
label.Text = Thread.CurrentThread.CurrentCulture.ToString();
The CurrentCulture is en-GB for both instances, so what is the problem here? Am I missing anything?
My local windows is Win7 SP1 with a german language-pack, the server is Win2008 R2 with standard English installation.
We had a similar problem on a customer server where the customer had manually changed the decimal separator symbol for a particular culture on that server. You can check this using:
Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator
I would suggest explicitly specifying the decimal separator symbol when you do the parsing:
Double.Parse("1,25", new NumberFormatInfo(){ NumberDecimalSeparator = "," });
Related
Some test code:
string Input = "13.00"
float Output = float.Parse(Input, new CultureInfo("en-US"));
On my PC (Win10 x64, English - US) the value of output is 13.
On another PC (Win10 x64) that I don't have access to where the language is US English but the region is Denmark, the value of Output is 1300.
I've tried to recreate the settings on my PC by swapping comma and decimal point and setting the system locale to Denmark but it always gives a value of 13 for me.
I've tried it on some German Win10 x64 PCs and they also set Output to 13.
I thought the purpose of specifying the culture was to force the use of the comma and decimal point to a specific usage?
Any ideas what I am doing wrong here? Thanks!
Update
Adding the following code:
string Debug = string.Format("{0:0.00}", Output);
results in Debug containing "13.00" on our test PCs (English and German) and "1300,00" on the problem PC.
Update 2
Changing from new CultureInfo("en-US") to CultureInfo.InvariantCulture seems to get the correct output of 13 on the problem PC.
What is the difference between en-US and invariant and why would that only manifest itself on this one example PC that we have seen so far?
I have a four textboxs, the first one sums a column from a gridview and the 2nd box should multiplies the first box by the last box, the third box should add box two and one. This works on my local pc yet on the server it gives me the error input is not in the correct format. Any help is welcome.
textbox24 = 1.14
textbox 21 = 5234
textbox 22= multiply box24 * box21
textbox 23 = box21 + box22
English - South Africa - both server and local pc
You are using ConvertToDouble before show the text in the textBox22
Your commented line is good, the one below the comment, no ;)
public void Multiply()
{
try
{
double vat = Convert.ToDouble(TextBox24.Text);
double tot = Convert.ToDouble(TextBox21.Text);
double ans = tot * vat;
//TextBox22.Text = ans.ToString().Trim();
TextBox22.Text = ans.ToString().Trim();
}
catch (Exception e)
{
TextBox22.Text = e.Message;
}
}
Marcus and Scott are right. This should be a culture difference between your local pc and your server. You can check them from Regional and Language Options part.
Convert.ToDouble method uses current thread culture.
From documentation;
Using the ToDouble(String) method is equivalent to passing value to
the Double.Parse(String) method. value is interpreted by using the
formatting conventions of the current thread culture.
That means if your textboxes have NumberDecimalSeparator or NumberGroupSeparator for your decimal values, and these properties are not equal for local pc's culture and your server culture, it is too normal to get FormatException.
But since you never mentioned about your textbox values, your local culture and your server culture, we never know what exact problem is.
They said, I can change the settings from the web.config file, does
anyone know how to do this ?
Read How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization
I am reading data from an API file, which has this format
<DataPoint>
<Amount>38.361</Amount>
<Time>2014-01-02T12:00:00</Time>
</DataPoint>
when I get the time, and print it at my website at visual studio I get 02/01/2014.
However, If I upload it, I get 1/2/2014.
Why the same code produces different results when I use it at my pc, and when I upload it at the server?
And how I can fix that?
ps: I am programming in C# and I am using the object JArray to get the data if this is important
JArray a = JArray.Parse(text);
But it
That's because of the DateTime CULTURE info. British dates are arranged as dd/MM/yyyy and American dates are MM/dd/yyyy You could just use Datetime.ToString("dd/MM/yyyy") to convert it to datetime format you want to display.
Format in which numeric and datetime values are transformed into string depends on OS culture settings, when you don't set them explicitly in code. Looks like that's the case here.
If you need the same date/numeric format everywhere, no matter how user set's the OS preferences, you should provide IFormatProvider instance which will provide the formatting and override OS default one.
The most commonly used is CultureInfo.InvariantCulture:
var dateString = myDate.ToString(CultureInfo.InvariantCulture);
I have a problem with converting string to double, it works fine when I run it on my local machine but when I deploy it to the server it "cuts" 0 in front.
For example I have a string value of 0,0123 and when I convert it to double I get 123.
I use ASP.NET with C#, method I use is Convert.ToDouble() and the hosting is somee.com.
Right now I just divide the number by 10000 and get what I need, but as you can imagine it's not prefect solution so maybe someone has better idea of how to solve it.
This happens because your machine and the server are using two different cultures.
Some cultures use "." as a decimal point, while others use ",".
It seems that in your server culture, "." is the decimal point - and so the comma in "0,0123" is treated as a thousands separator instead.
I would use the overload for double.TryParse or Convert.ToDouble that takes an IFormatProvider and pass in the correct CultureInfo.
In the future, try using CultureInfo.InvariantCulture to format all your internal data (i.e. data persistence, or data flowing within your system), and use the proper CultureInfo for displaying data to the user.
The decimal point is not the same across cultures and, therefore, computers with different culture setting.
Try using double.Parse(string, CultureInfo) with an explicit CultureInfo that works for your format.
Based on the fact you express your value as 0,0123 rather than 0.0123, I would guess the language/culture settings on the server are different than on your own machine.
Try printing out the result of System.Globalization.CultureInfo.CurrentCulture.Name from somewhere on the server, and compare it to what your local machine produces.
I am wondering what is the best way to figure out how to convert a datetime?
Right now my computer is setup as dd/mm/yyyy yet I am taking date as mm/dd/yyy so when I try to do
DateTime.Convert();
DateTime.Parse();
DateTime.TryParse();
I either get nothing back or it crashes. Now I could tell it about the mm/dd/yyyy format and it probably would convert. However the problem is these dates are are coming from user uploaded files.
They could be in any format combination. I am trying to find ways to do it.
The problem I see is that I am looking at the dates in an uploaded file so I am not sure if looking say at the browser date format would help or not.
I am using asp.net mvc and I am trying to get a solution that handle date formats automatically so I don't have to ask the user about the date format they have (I figure the more things I have to ask them the less likely the user will continue on)
No, you can't figure out automatically what date-time format a user meant to use once the value is on the server. You need more information to parse it correctly (e.g. 1/2/3 can mean a lot of different dates depending on the culture).
Consider one of the following solutions:
Convert the entered date to a text representation in a standard format (i.e. ISO 8601 - 2012-02-09) using JavaScript on the client before you send it to the server. The code would look something like this: d.getUTCFullYear()+"-" + d.getUTCMonth() + "-" + d.getUTCDate().
Send the local culture information to the server along with date value to be converted and do the conversion on the server.
Force the user to enter the date in a specific format (e.g. Use 3 text boxes labeled "Month", "Day", and "Year" instead of one text box with free input).
chobo2 (I like the 'handle') :)
you can detect the locale culture and work on that at will. see the following SO Q/A for pointers:
Where is the system locale/culture set for .Net
the key is to NOT have to set anything in particular, but identify the locale and act accordingly.