| | |
Floating point numbers
![]() |
•
•
Join Date: Apr 2005
Posts: 5
Reputation:
Solved Threads: 0
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
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".
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
-->sometimes i wanna take my toaster in a bath<-- 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.: will display
will display [code]"69.27"[code]
C Syntax (Toggle Plain Text)
%1f
C Syntax (Toggle Plain Text)
"69.3"
C Syntax (Toggle Plain Text)
%2f
-->sometimes i wanna take my toaster in a bath<-- ![]() |
Similar Threads
- Floating point multiplication, precision issues (C++)
- floating point : overflow error (C)
- Dynamic Array, Writing to CSV, Floating Point ?'s (C)
- FLOATING POINT.......Please (C++)
- IEEE Floating-point fromat (C)
- Floating point numbers (C++)
Other Threads in the C Forum
- Previous Thread: segfault and valarray
- Next Thread: Dialog Box problems
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






