| | |
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,757
Reputation:
Solved Threads: 283
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
Views: 2723 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets







