I have a calculation for example 2/4 = 0.5, except I can’t find a data type that will store this value! Every time I make the calculation it says 0.
Anyone have any suggestions?
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.
Either
doubleordecimalwill work fine.However, if you just write:
then it will still use integer division – because both of the operands for the division operator are of type
int.You can either cast either or both of the operands to double, or use a double literal for either or both of them:
Note that both
doubleanddecimalare floating point types. They don’t represent arbitrary rational numbers (fractions). For example, neither can accurately represent 1/3. If you need a rational number type, you’ll have to write your own or look for a third party implementation.