Floating point numbers

Reply

Join Date: Apr 2005
Posts: 5
Reputation: monkeyhead is an unknown quantity at this point 
Solved Threads: 0
monkeyhead monkeyhead is offline Offline
Newbie Poster

Floating point numbers

 
0
  #1
Apr 26th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Floating point numbers

 
0
  #2
Apr 26th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 5
Reputation: monkeyhead is an unknown quantity at this point 
Solved Threads: 0
monkeyhead monkeyhead is offline Offline
Newbie Poster

Re: Floating point numbers

 
0
  #3
Apr 26th, 2007
Code Attached,
Variable in question is "average"
Many thanks
Matt
Last edited by monkeyhead; Apr 26th, 2007 at 4:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Floating point numbers

 
0
  #4
Apr 26th, 2007
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 5
Reputation: monkeyhead is an unknown quantity at this point 
Solved Threads: 0
monkeyhead monkeyhead is offline Offline
Newbie Poster

Re: Floating point numbers

 
0
  #5
Apr 26th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 29
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Floating point numbers

 
0
  #6
Apr 26th, 2007
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<--
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,429
Reputation: Nichito is an unknown quantity at this point 
Solved Threads: 29
Featured Poster
Nichito's Avatar
Nichito Nichito is offline Offline
Nearly a Posting Virtuoso

Re: Floating point numbers

 
0
  #7
Apr 26th, 2007
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
  1. "69.3"

will display [code]"69.27"[code]
-->sometimes i wanna take my toaster in a bath<--
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC