1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #include <iostream> #include<afx.h> #include<afxwin.h> #include<math.h> CString GetStrDataValue(double dataValue) { CString strResult = ""; if (dataValue == 0) { strResult.Format("0"); } else if (dataValue - floor(dataValue) <= DBL_EPSILON) { // 정수 strResult.Format("%.0lf", dataValue); } else { // 정수가 아닌 실수 if (dataValue < pow(10.f, -6)) { // 10e-6보다 작은 실수일때 for (int i = -10; i < 0; i++) { if (abs(dataValue - floor(dataValue)) < pow(10.f, i)) { CString strFormat = ""; strFormat.Format("%%.%dlf", 6 - i); strResult.Format(strFormat, dataValue); break; } } } else { strResult.Format("%lf", dataValue); } int strLen = strResult.GetLength(); int nDecimalPoint = strResult.Find(".", 0); for (int strPos = strLen-1; strPos > nDecimalPoint; strPos--) { if (strResult.GetAt(strPos) != '0') { strResult = strResult.Left(strPos+1); break; } } } return strResult; } int main() { CString strFormat = ""; CString strResult = ""; strResult = GetStrDataValue(2.5); printf("CString: %s\t\t\t\t\t\tdouble: %lf\n", strResult, 2.5); strResult = GetStrDataValue(59.99999); printf("CString: %s\t\t\t\t\tdouble: %lf\n", strResult, 59.99999); for (int i = 0; i < 10; i++) { strResult = GetStrDataValue(pow(10.f, -i)); switch (i) { case 0: case 1: case 2: case 3: case 4: printf("CString: %s\t\t\t\t\t\tdouble: %lf\n", strResult, pow(10.f, -i)); break; case 5: printf("CString: %s\t\t\t\t\tdouble: %lf\n", strResult, pow(10.f, -i)); break; default: printf("CString: %s\t\t\t\t\tdouble: %lf\n", strResult, pow(10.f, -i)); break; } } strResult = GetStrDataValue(pow((double)2, -32)); printf("CString: %s\t\t\t\tdouble: %lf\n", strResult, pow((double)2, -32)); strResult = GetStrDataValue(6400); printf("CString: %s\t\t\t\t\t\tdouble: %lf\n", strResult, (double)6400); strResult = GetStrDataValue(4294967295); printf("CString: %s\t\t\t\t\tdouble: %lf\n", strResult, (double)4294967295); strResult = GetStrDataValue(-1571); printf("CString: %s\t\t\t\t\t\tdouble: %lf\n", strResult, (double)-1571); strResult = GetStrDataValue(FLT_MAX); printf("CString: %s\tdouble: %lf\n", strResult, FLT_MAX); strResult = GetStrDataValue(-3.402823466e+38F); printf("CString: %s\tdouble: %lf\n", strResult, -3.402823466e+38F); } | cs |
흡-족 ㅋㅋ. 더 쉬운 방법이 있을지도... =ㅅ=
이틀동안 머리깨지는 줄 알앗내 ㅡㅡ
strString.TrimRight('0');
strString.TrimRight('.');
이러면 자투리 다 짤리지 않던가요?
과제용이면 뭐 직접 만드시는게 답이시겠지만...