The Daily WTF
Matt‘s co-worker needed to handle some currency values coming in as strings. As this was in C++, we’re already in a fraught territory of having to worry about whether the callers and various APIs we’re using handle C-style strings or C++ std::string.
Or, you can just mix-and-match in code and hope for the best, while having some interesting approaches to handling your inputs. That’s what this developer did.
int Amount(char *value,int len) { char *scanp = value; string s; float f; long l; if (strchr (scanp,’.’)) { f = atof(value); f *= 100; f /= 100; l = (int)f; } else { // if the decimal was not found l = atol(value); } sprintf(value,”%li”,l); s = value; while(s.length() < len) s.insert(0,”0″); if (s.length() > len) { char szTemp[256]; memset(szTemp,0,sizeof(szTemp)); strncpy(szTemp,s.c_str(),len); strcpy(value,szTemp); } else strcpy(value,s.c_str()); return 1; }
There’s a lot going on here. The purpose of this function is
To read the full article click on the 'post' link at the top.