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?
You could just add a decimal to either the numerator or the denominator:
double result = 30.0 / 233;
double result = 30 / 233.0;
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:
sprintf(str, "result = %.2f", result);
Bascially, the ".2" represents how many digits to output after the decimal point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With