Question

I have code that is taking information from the database to insert into a list. One piece of data that I'm taking will be used for price. (I set its type to decimal in SQL Server), for example: " 1,80".

But when I select it, my decimal variable returns the value 2 !

I want to know if there are a simple way to make decimal not automatically round numbers.

This is the code:

public decimal preco { get; set; } 

while (dr.Read())
{
    clsCaProd oProd = new clsCaProd();
    oProd.cod = dr.GetInt32(0);
    oProd.preco = dr.GetDecimal(1); // Here returns "2" instead of "1,80"
    oProd.info = dr.GetString(2);
    oProd.categ = dr.GetString(3);
    oProd.nome = dr.GetString(4);

    lProd.Add(oProd);
}
Was it helpful?

Solution

If you declare a column as decimal, the default "scale" is zero. You need to specify the precision and scale.

Try this example:

declare @d1 decimal = 1.80
declare @d2 decimal(19,4) = 1.80

select @d1, @d2;

Results:

2        1.8000
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top