How can I get a float or real value from integer division? For example:
double result = 30/233;
yields zero. I’d like the value with decimal places.
How can I then format so only two decimal places display when used with a string?
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.
You could just add a decimal to either the numerator or the denominator:
Typecasting either of the two numbers also works.
As for the second part of the question, if you use printf-style format strings, you can do something like this:
Bascially, the ‘.2’ represents how many digits to output after the decimal point.