Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5993839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:43:57+00:00 2026-05-22T23:43:57+00:00

Based on my tests, casting a decimal or double to an int or long

  • 0

Based on my tests, casting a decimal or double to an int or long simply keeps the whole number portion of the number and discards the decimal portion. Reversely, you can have an int variable and assign it directly to a decimal variable without casting and it will perform an implicit conversion.

It works, but just because it works doesn’t always mean it’s the best way. Are there any performance implications over this type of conversion or is it best to use Convert.ToWhatever()?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:43:58+00:00Added an answer on May 22, 2026 at 11:43 pm

    You require the cast for decimal to int or double to int because the conversion results in a loss of precision, therefore rather than allowing you to inadvertently perform a lossy operation you are expected to be explicit about it.

    Going the other way around, a double or decimal can fully represent any value assigned from an int and therefore the conversion can be done implicitly on your behalf.

    The only question is, is the cast doing the kind of conversion you require, ie. is simply truncating the fractional portion sufficient or do you require specific rounding etc.

    In terms of performance, the cast of a double to int simply uses the conv.i4 opcode where using something like Convert.ToInt32(double) results in a function call to perform the conversion.

    For decimal to int the cast will result in a call to an operator overload, essentially a function call, and of course using Convert.ToInt32(decimal) is again a function call.

    In the case of decimal there is essentially no difference, but Convert.ToInt32(double) will not even be inlined so it is considerably more expensive, but is the cost significant in the bigger picture, typically not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any web-based IDE that I can use to run quick tests? There're
I have problem organizing my unittest based class test for family of tests. For
Is there any website which provides core java based programming challenges that tests programming
I'm currently working on a Haskell project which automatically tests some functions based on
I'm using TestNG to run Selenium based tests in Java. I have a bunch
I am trying to get Hudson to run my ruby based selenium tests. I
I have time based tests to run that require changing the system time multiple
Open Source projects that ship with unit tests based on NUnit also usually ship
The selenium tests I'm gonna be doing are basically based on three main steps,
I'm in process of using EasyMock to write Unit tests for a number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.