views:

85

answers:

4

I have a value that is correctly stored in a property of an object, but when I save the changes to the Azure storage database, the double value is stored to the database ignoring the point (7.11000000003 is saved as 711). Also, the property is changed to 711.0.

How do I solve this problem?

The field is already set to double in the class and the database table.

+3  A: 

Is your double value in its own field, or is it in the partitionkey or rowkey field? PartitionKey and RowKey are always strings.

I just created a simple test which writes and reads a row with a double field, and the values are preserved just fine. I modified the sample SmsMessage class from Bill Lodin's SMS code via msdev.com training (I took the int Delay field and changed it to a float, and renamed it to MyDouble for clarification):

public class SmsMessage: TableServiceEntity
{
    public double MyDouble { get; set; }
    public SmsMessage(string destination, string message, double myDouble)
    {
        PartitionKey = destination;
        RowKey = message;
        MyDouble = myDouble;
    }
    public SmsMessage()
        : base("", string.Format("{0:d10}", DateTime.Now.Ticks))
    {
    }
}

I then write to the SmsMessageTable:

smsTable.AddObject("SmsMessages", new SmsMessage(destination, message, myDouble));
smsTable.SaveChanges();

I view this in the table storage explorer, and my doubles are how I entered them (e.g. 1.2345).

I then retrieve with a simple linq query, for a given username in the partition key:

var results = from m in smsTable.SmsMessages
                      where m.PartitionKey.Equals(txtDestination.Text.Trim())
                      select m;

My double values are all preserved and strongly typed as doubles.

David Makogon
Yes, that seems to be correct! The SaveChanges() method should not alter the values of the properties, but anyway it makes me believe that there could some constraint that doesn't allow the double to be saved as such. David, do you have any suggestion about this problem?
Fabio Milheiro
Hi Fabio - I don't know of any restriction on double. The example I gave lets me save and retrieve doubles with no issues whatsoever. Have you tried creating a very simple TableServiceEntity-derived class like the one above? Also: after writing to storage, try viewing your data with a storage explorer (such as the one built into VS2010). If you're not seeing doubles, that means there's probably an issue with your TableServiceEntity.
David Makogon
We use a class BaseEntity equivalent to your TableServiceEntity and the problem must be there. And no one in this company seems to figure it out. I'll try your code just to make sure that problem is in our BaseEntity class and then check it. Thank you David!
Fabio Milheiro
+1  A: 

How are you checking the value? Local development storage uses SQL Express under the hood, so you might be opening up SQL and poking around there. I would avoid that and instead read back the value using the .NET storage client library. I suspect the value will come back correctly.

(Maybe in the local SQL-backed store, the value is stored in scientific notation?)

smarx
I know that the local development storage uses SQL Server Express and I understand what you mean, but this is very strange, because the value itself is changed immediately after saving the data to the database.Forget the value stored in the database. The property double looses the decimal point. For example, this 7.11000003 becomes 711.0 in the application! It is NOT in scientific notation. Do you have any other suggestion? It's always welcome! :)
Fabio Milheiro
I see. I have no idea why that would happen... have you tried David's code? Does it work for you?
smarx
No, but I will. I suspect now that our problem is in our BaseEntity because other members in the company have the same problem and could not solve it yet. Thank you smarx!
Fabio Milheiro
A: 

Although I appreciate the effort and probably I did not expose the case well enough, none of this really answer my question. This answer is just not to attribute the bounty unfairly. The bounty should not be attributed automatically to the best answer with >= 2 upvotes.

Fabio Milheiro
You might want to consider publishing your code for inspection. Without seeing what you actually coded, it's really hard to diagnose, especially since none of us have been able to recreate the problem.
David Makogon
Thank you, but I found that the problem is not in the code. It's always been correct. It doesn't work on local storage, but works perfectly on Azure remote storage. The reason why is something that I don't know yet, but not very concerned about.
Fabio Milheiro
+1  A: 

This problem seems to be due to the culture settings the Dev Storage is using. If you take a look at TableRow table inside the Dev Storage db, the data is stored as XML, and decimal values use the dot as decimal separator. Here in Brazil the dot is the thousands separator. I edited the data directly in the Dev Storage db, changing the dot to a comma (PT-BR decimal separator), and the value was read ok. That's odd, because if the value is read ok when we use the PT-BR decimal separator, it seems Dev Storage is using my current culture setting. But why the culture is not applied on saving the data?

PS: Changing the windows locale to EN-US solved the problem. I guess that's why this is the only post I could find about it.

GBUchoa
Thanks GBUchoa!
Fabio Milheiro