| | |
round off number
![]() |
•
•
Join Date: Sep 2006
Posts: 26
Reputation:
Solved Threads: 0
how will i round off the return value to decimal place with tis function?
C Syntax (Toggle Plain Text)
unsigned int CalculatePercentage(int TotalComponent, int PartialComponent){ return int((PartialComponent/TotalComponent)100); }
•
•
•
•
how will i round off the return value to decimal place with tis function?
C Syntax (Toggle Plain Text)
unsigned int CalculatePercentage(int TotalComponent, int PartialComponent){ return int((PartialComponent/TotalComponent)100); }
*). If you want to win, you must not loose (Alan Ford)
Try:
floatValue = float(int(floatValue * 10 + 0.5)) / 10; Please anyone, correct me if I am wrong. It is the best way for me to learn.
•
•
•
•
okie thanks but how can i round it off to one deciamal place?
for example
(PartialComponent/TotalComponent)*100
90/235=38.3 %
38.3 should be the return value
anyone? i shouldnt be using any math.h function etc.
If you want to win, you must not loose (Alan Ford)
The rounding is done in your display function. Here is C example:
And here is C++ example:
C Syntax (Toggle Plain Text)
// percent calculation, wants 100 * 90/235 --> 38.3 % #include <stdio.h> double CalculatePercentage(double TotalComponent, double PartialComponent) { return 100*PartialComponent/TotalComponent; } int main() { printf("100 * 90/235 --> %0.1f%c", CalculatePercentage(235, 90), '%'); getchar(); // wait for key press return 0; }
C Syntax (Toggle Plain Text)
// percent calculation, wants 100 * 90/235 --> 38.3 % #include <iostream> #include <iomanip> using namespace std; double CalculatePercentage(double TotalComponent, double PartialComponent) { return 100*PartialComponent/TotalComponent; } int main() { cout.setf(ios::fixed); cout<< setprecision(1) << "100 * 90/235 --> " << CalculatePercentage(235, 90) << '%' << endl; cin.get(); // wait for key press return 0; }
Last edited by bumsfeld; Oct 13th, 2006 at 4:31 pm.
This does not make any sense, if the return value is double or float you can not guarantee precision to be 38.3 it most like will be 38.2999999 or something like that.
Try out something like this, though its a bit round about way, but should work:
Hope it helped, bye.
C Syntax (Toggle Plain Text)
double return_round( double value, double total ) { char buffer[BUFSIZ] = {'\0'} ; double percentage = ( value / total ) * 100 ; printf( "\nActual number: %lf", percentage ) ; sprintf(buffer, "%0.2f", percentage) ; if ( buffer[ strlen( buffer ) - 1 ] >= '5' ) buffer[ strlen( buffer ) - 2 ] += 1 ; buffer[ strlen( buffer ) - 1 ] = '\0' ; printf( "\nThe rounded string is: %s", buffer ) ; return atof( buffer ) ; }
Hope it helped, bye.
Last edited by ~s.o.s~; Oct 13th, 2006 at 5:16 pm.
I don't accept change; I don't deserve to live.
I'm with bumsfeld on this one. You want as much precision as you can get throughout the program's calculations and only round for the display. You don't even want to round the number itself, just the display.
If you round prematurely, all your calculations will be slightly off. And the more calculations you do, the more inaccurate your answer.
If you round prematurely, all your calculations will be slightly off. And the more calculations you do, the more inaccurate your answer.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- VB 2005 Trigonometric Functions (VB.NET)
- seperate float's decimal portion (C)
- Computer Architecture Reference (Computer Science)
- Round Numbers (JavaScript / DHTML / AJAX)
- !!! Help ME !!!!! (Pascal and Delphi)
- floating point : overflow error (C)
- Creating simple array... headache. (Java)
Other Threads in the C Forum
- Previous Thread: Other loop than 'for'?
- Next Thread: Hi, would you mind helping me with my homework?
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






