decimal value converted to string

69 views
Skip to first unread message

Riccardo Poli

unread,
Mar 23, 2016, 7:22:39 AM3/23/16
to mongodb-user
                Entita.FATTURA fa = new Entita.FATTURA()
                {
                    anno = fatt.Anno,
                    importotot = Convert.ToDouble(fatt.ImportoTot),
                    mese = fatt.Mese,
                    numfattura = fatt.NumFattura,
                    pagamento = fatt.Pagamento,
                    tipo = fatt.Tipo,
                    vocifattura = (from x in vocifattura
                                   where x.FatturaId == fatt.Id
                                   select new Entita.VOCI_FATTURA()
                                   {
                                       Id = ObjectId.GenerateNewId(),
                                       descrizione = x.Descrizione,
                                       importo = Convert.ToDouble(x.Importo)
                                   }).ToList(),
                    clientefattura = elenco_cli.Where(x => x.partitaiva == cl.PartitaIVa).FirstOrDefault()
                };

                _mongodb.GetCollection<Entita.FATTURA>("fattura").InsertOne(fa);

when i try to insert the document fa, is saved correctly, the problem is that importotot are saved as string, so, if i try to make $sum of importotot return 0, becouse converted to string from InsertOne()
fatt.ImportoTot is decimal, i must set fa.ImportoTot also double & convert fatt.ImportoTot do double, like this is saved correctly to database

is bug of driver? i use mongo 3.2 & driver for c# 2.2.3.3

Robert Stam

unread,
Mar 23, 2016, 12:17:49 PM3/23/16
to mongod...@googlegroups.com
There is no BSON type that corresponds to C#'s decimal type. In order to round trip a decimal value faithfully the decimal value is stored as a string in the database.

If you want to perform server-side arithmetic on the value in the database you would have to store it as a double, **but** that will result in rounding errors so you probably don't want to do that either.

There are plans to add decimal128 support in a future version of the server, at which point the C# driver will likely change to store C# decimal values as BSON decimal128 values.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.org/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/60ae0c04-8395-4db1-b9e8-1440242f96cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Riccardo Poli

unread,
Mar 24, 2016, 3:23:23 PM3/24/16
to mongodb-user
i hope support for decimal128 arrive soon, strange for me see that there aren't support for it, but for double yes
Reply all
Reply to author
Forward
0 new messages