## 2 replies

Last post Oct 16, 2018 03:16 AM by Zhi Lv - MSFT

• JagjitSingh

Member

504 Points

1776 Posts

### Decimal Value

Hi

In Database i have value 59.13267 . In C# it shows me 5900000 not 5913267 when multiplied by 100000. I have variable of type Decimal

Thanks

• PatriceSc

All-Star

48080 Points

17854 Posts

### Re: Decimal Value

Hi,

This is exactly what would happen if the decimal value is converted to an integer before doing the calculation so more likely this is what happens.

Show maybe how you read this decimal value from your database? You are 100% sure the final result still uses the decimal type ???

From past posts I suspect you are using perhaps something such as Convert.ToInt32(dt.Rows[0]["Value"]) * 100000

The problem with using Convert this way is that it won't care if you mistakenly convert a decimal to an int. If confirmed you could use another approach to avoid introducing conveesion mistakes in your code.

Edit: for example with System.DataSetExtensions.dll you could write :

dt.Rows[0].Field<decimal>("Value") to return a decimal value.

The benefit is that dt.Rows[0].Field<int>("Value") would through an exception if the underlying column is a decimal because rather than "forcing" a conversion to a particular type you are just "casting" the column back to its actual underlying type and so it would be harder to mistakenly write (or at least run) wrong code.

• Zhi Lv - MSF...

All-Star

45489 Points

7008 Posts

Microsoft

### Re: Decimal Value

Oct 16, 2018 03:16 AM|Zhi Lv - MSFT|LINK

Hi JagjitSingh,

#### JagjitSingh

In Database i have value 59.13267 . In C# it shows me 5900000 not 5913267 when multiplied by 100000. I have variable of type Decimal

As PatriceSc said, please check your code, whether you have converted the decimal data to Int32.

Try to set break point to debug your code and step by step to check the value (check the SQL query result, and the related code about the convert part of code).