Advertisement
Guest User

ugly converter, very ugly

a guest
Nov 22nd, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. string shortenedDouble(double number)
  6. {
  7.     ostringstream strs;
  8.     strs << number;
  9.     string shortened = strs.str(); //take a string from our ostringstream
  10.     int dot_location;
  11.     dot_location = shortened.find('.');
  12.     if (dot_location == -1){return shortened;} //if dot not found at all
  13.     else{return shortened.substr(0, dot_location+2);}
  14.  
  15. }
  16.  
  17. int main()
  18. {
  19.     double a = 4.2342;
  20.     double b = 3.42221;
  21.     double c = 33.123;
  22.  
  23.     cout << shortenedDouble(a) << endl;
  24.     cout << shortenedDouble(b) << endl;
  25.     cout << shortenedDouble(c) << endl;
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement