Anyone have any idea how I would write a program that lets user input a floating point number and then either round up if number is like 4.5 our round down(truncate) if number like 4.49. Any help would be greatly appreciated.
csaund1 0 Newbie Poster
Recommended Answers
Jump to PostThere are several ways that can be done. One way is to add 0.5 (or a similar value depending on where you want to round) to the original value and then cast it to an int and then back again if you want to round to the nearest decimal value …
Jump to PostWARNING:Code based on my logic. Not compiled and tested in reality.
//round to closest integer.
double d = 2.49;
int i = (int)(d + 0.05);
cout << i << endl;d = 2.51;
i = (int)(d + 0.05);
cout << i << endl;//round to closest tenth …
Jump to Post>>Lerner, check up your code again. It's wrong
Yup; looks like this,
i = (int)(d + 0.05);
should be this:
i = (int)(d + 0.5);
Any other (il)logic errors (and there may be some) will need to wait for me to check it with a …
All 9 Replies
Lerner 582 Nearly a Posting Maven
csaund1 0 Newbie Poster
Lerner 582 Nearly a Posting Maven
ArkM 1,090 Postaholic
ArkM 1,090 Postaholic
galin 0 Newbie Poster
Lerner 582 Nearly a Posting Maven
ArkM 1,090 Postaholic
ArkM 1,090 Postaholic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.