decimal.TryParse() drops leading "1"
- by Martin Harris
Short and sweet version:
On one machine out of around a hundred test machines decimal.TryParse() is converting "1.01" to 0.01
Okay, this is going to sound crazy but bare with me...
We have a client applications that communicates with a webservice through JSON, and that service returns a decimal value as a string so we store it as a string in our model object:
[DataMember(Name = "value")]
public string Value { get; set; }
When we display that value on screen it is formatted to a specific number of decimal places. So the process we use is string - decimal then decimal - string.
The application is currently undergoing final testing and is running on more than 100 machines, where this all works fine. However on one machine if the decimal value has a leading '1' then it is replaced by a zero. I added simple logging to the code so it looks like this:
Log("Original string value: {0}", value);
decimal val;
if (decimal.TryParse(value, out val))
{
Log("Parsed decimal value: {0}", val);
string output = val.ToString(format, CultureInfo.InvariantCulture.NumberFormat);
Log("Formatted string value: {0}", output);
return output;
}
On my machine - any every other client machine - the logfile output is:
Original string value: 1.010000
Parsed decimal value: 1.010000
Formatted string value: 1.01
On the defective machine the output is:
Original string value: 1.010000
Parsed decimal value: 0.010000
Formatted string value: 0.01
So it would appear that the decimal.TryParse method is at fault.
Things we've tried:
Uninstalling and reinstalling the client application
Uninstalling and reinstalling .net 3.5 sp1
Comparing the defective machine's regional settings for numbers (using English (United Kingdom)) to those of a working machine - no differences.
Has anyone seen anything like this or has any suggestions? I'm quickly running out of ideas...
While I was typing this some more info came in: Passing a string value of "10000" to Convert.ToInt32() returns 0, so that also seems to drop the leading 1.