944,149 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1261
  • C RSS
Apr 26th, 2007
0

Floating point numbers

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
monkeyhead is offline Offline
5 posts
since Apr 2005
Apr 26th, 2007
0

Re: Floating point numbers

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".
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 26th, 2007
0

Re: Floating point numbers

Code Attached,
Variable in question is "average"
Many thanks
Matt
Last edited by monkeyhead; Apr 26th, 2007 at 4:59 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
monkeyhead is offline Offline
5 posts
since Apr 2005
Apr 26th, 2007
0

Re: Floating point numbers

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 26th, 2007
0

Re: Floating point numbers

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
monkeyhead is offline Offline
5 posts
since Apr 2005
Apr 26th, 2007
0

Re: Floating point numbers

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.
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Apr 26th, 2007
0

Re: Floating point numbers

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.:
  1. %1f
will display
  1. "69.3"

  1. %2f
will display [code]"69.27"[code]
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: segfault and valarray
Next Thread in C Forum Timeline: Dialog Box problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC