| | |
Help with Rounding 3 decimal places
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 56
Reputation:
Solved Threads: 0
Hey guys, this function is for rounding 2 decimal places, e.g. 1.2785 rounds to 1.2800. First, how would I alter the function so it would round 3 decimal places 1.2780, and second how would I truncate it so it would read 1.278.
Thanks!
Thanks!
C++ Syntax (Toggle Plain Text)
double roundIt(double x, double n) //Write definition for rounding function { x = floor( x * pow(10.0, n) + 0.5) / pow(10.0, n); return x; }
•
•
Join Date: Jul 2005
Posts: 1,699
Reputation:
Solved Threads: 272
play around with the value of n and see what happens. In particular, try changing n from 1 to 2 to 3 etc. The function as written isn't specific for rounding to 2 decimal places.
Also, look into the stream manipulators, and here I'm thinking of the fixed and setprecision manipulators, to determine how many decimal points to display.
Also, look into the stream manipulators, and here I'm thinking of the fixed and setprecision manipulators, to determine how many decimal points to display.
Last edited by Lerner; Apr 2nd, 2009 at 4:11 pm.
Klatu Barada Nikto
well at the first place i would recommend usage of float data type instead on double , guess float would do the trick.
second of all subtract the left part of the no from the original no itself , by doin this u can isolate the decimal part and then if u r looking for a 3 decimal round off in all the nos ( which apparently is ) .
u can multiply the 3 digits decimal into 100 and then round of them
well i would like you to dry run the code .
second of all subtract the left part of the no from the original no itself , by doin this u can isolate the decimal part and then if u r looking for a 3 decimal round off in all the nos ( which apparently is ) .
u can multiply the 3 digits decimal into 100 and then round of them
C++ Syntax (Toggle Plain Text)
float round( float no ) { int x, temp; float y; temp = x = no ; only the integer part is taken y = no - x ; u get the decimal number here y*=100 ; decimal gets converted to 2 digit no n 1 decimal x = y; y-= x; the last decimal is stored in y if( y > = 0.5 ) x++ ; return (temp + x/100) ; }
well i would like you to dry run the code .
![]() |
Similar Threads
- Need Help Rounding Off (C++)
- rounding a double before cout ? (C++)
- Rounding function (C++)
- Problem with rounding off values (VB.NET)
- Rounding numbers problem (Visual Basic 4 / 5 / 6)
- Rounding numbers (JSP)
- define variable (C)
- NumberFormat - Incorrect rounding (Java)
- rounding number to specific decimal place (C)
Other Threads in the C++ Forum
- Previous Thread: VC++ programmatically scroll down in listview
- Next Thread: C++ Program Hangs
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets







