| | |
round off number
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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.
The question is about rounding off the returned value and not rounding off while displaying. Read the first post.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
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?
Views: 9413 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab win32 windows.h






