Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- using namespace std;
- string shortenedDouble(double number)
- {
- ostringstream strs;
- strs << number;
- string shortened = strs.str(); //take a string from our ostringstream
- int dot_location;
- dot_location = shortened.find('.');
- if (dot_location == -1){return shortened;} //if dot not found at all
- else{return shortened.substr(0, dot_location+2);}
- }
- int main()
- {
- double a = 4.2342;
- double b = 3.42221;
- double c = 33.123;
- cout << shortenedDouble(a) << endl;
- cout << shortenedDouble(b) << endl;
- cout << shortenedDouble(c) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement