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 PostYOU show us some code.
Consider a proper using of floor() library function.
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.