I have a double and need to convert it to a decimal. If the value is too large/small the result should be decimal.MaxValue/decimal.MinValue. What is the best way to do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
I assume you realize this is a lossy conversion, and should be avoided if possible? If you really need it:
This way the cap is checked in terms of the
doublevalue, but then return the capped value hard-coded to be the decimal value. This avoids the problem wheredoublecan’t accurately represent the max/min values ofdecimal.(Even at the largest scales of
decimal, consecutive integers can be represented – but that’s not the case fordouble.)