none
c# Divide operation Not working RRS feed

  • Question

  • When I do this math below, I do not receive the decimal point.

    string test = (2456 / 1000).To.String();

    I get 2 instead of 2.456. How can I fix this issue. The answer suppose to be 2.456. Can someone assist me.


    pianoboyCoder

    Thursday, November 16, 2017 6:08 PM

All replies

  • Try below code

    string test = ((double)2456 / 1000).To.String();

    And you can also refer following link

    https://www.dotnetperls.com/divide


    Gaurav Khanna | Microsoft MVP | Microsoft Community Contributor

    • Proposed as answer by Ashish Pandey Friday, November 17, 2017 4:37 AM
    Thursday, November 16, 2017 6:22 PM
  • Hello pianoboyCoder,

    You can't get decimal data by dividing two integers, you need to convert the one of them to float type at least.

     
    string test = (2456f / 1000).ToString();

    Note. The 'f' has specified the number parsed by float type.

    https://stackoverflow.com/questions/661028/how-can-i-divide-two-integers-to-get-a-double

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 17, 2017 6:50 AM
    Moderator
  • Hello pianoboyCoder,

    Do you have anything to update?  If you already have got the useful solution from there, you could close the thread by marking the useful reply as answer, This can be beneficial to other community members reading this thread. Thanks a lot.

    Sincerely,
    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, November 26, 2017 11:46 AM
    Moderator