Hi there,
I've written a program that calculates the average of a few things. How ever somtimes the average can be say for for example 69.3%. If I was to get that value to show, would I use a float or a double?
I've tried using a float but it would only display the value as 69 and not the full value?
Not quite sure how to go about this?
Any suggestions would be appreciated!
Many thanks
Matt Seymour

post your code. It doesn't matter whether it is a float or double -- the problem is not the data type but how you are trying to display it. If this is a C program and you are using printf(), then the format string needs to be "%.1f", not "%d".

Code Attached,
Variable in question is "average"
Many thanks
Matt

I don't see the line that is displaying the average, but I suspect the problem may be here:

>>average=grand_total/x;
since grand_total and x are both integers the vaue of average will also be an integer. typecase the two integers to float and the compiler will use floating point math, not integer math.

Ah I see! Thanks for the advice, got what I wanted to achieve! However is there a way to reduce the figures to say 2d.p?
Many thanks for the help so far,
Matt

when you use the library stdlib for the input/output commands, and you need to print decimals, you need to be careful on printing (as AD says) %1f or %2f (to display hundredths), instead of %d, since in %d it only displays whole values, not partial ones.

about reducing the decimal points, if you include numbers before the f in %f, you will obtain the amount of digits after the period as you specified. i.e.: %1f will display "69.3"

%2f will display "69.27"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.