locked
Decimal Value RRS feed

  • Question

  • User-1499457942 posted

    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

    Monday, October 15, 2018 5:33 PM

Answers

  • User753101303 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2018 5:49 PM
  • User-271186128 posted

    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).

    If the problem still not solved, please post the related code about this part, it might be easier for us to help you solve the problem.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 16, 2018 3:16 AM

All replies

  • User753101303 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2018 5:49 PM
  • User-271186128 posted

    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).

    If the problem still not solved, please post the related code about this part, it might be easier for us to help you solve the problem.

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 16, 2018 3:16 AM