Bloomberg LP Interview Question
Financial Software DevelopersAnd here is one using the STL:
#include <iostream>
#include <sstream>
#include <string>
#include <locale>
using namespace std;
void to_dollar(int a);
int main()
{
int userInput;
cout << "Please enter an integer-:> " << endl;
cin >> userInput;
to_dollar(userInput);
return (0);
}
void to_dollar(int a)
{
stringstream inString;
string strTemp;
long double intTemp;
inString << a; // convert integer to string for easier handling
inString.str(inString.str()+"00"); // concatenate two zeos to denote currency fractional digits
inString >> intTemp; // convert back to integer
locale loc("english_USA"); // set the locale
cout.imbue(loc);
cout.setf(cout.flags() | ios::showbase); // turn on currency sign printing
const money_put<char>& m_put = use_facet<money_put<char> >(loc); // get the money_put facet
m_put.put(cout, false, cout, ' ', intTemp); // show me the money. Note to use fractionally adjusted value.
cout << endl;
}
int my_number_format(int num)
{
int idx;
char buffer[64];
idx = sizeof(buffer)-1;
buffer[idx--] = 0; // end zero
int count = 0;
while (num > 0) {
if (0 == idx)
return -1;
if (count > 2) {
buffer[idx--] = ',';
if (0 == idx)
return -1;
count = 0;
}
count++;
buffer[idx--] = '0'+(num % 10);
num /= 10;
}
buffer[idx] = '$'; // currency sign
return printf("%s\n", &buffer[idx]);
}
We may code it using stack
void _dollar(int x)
{
char s[64];
stack stk;
*s = '\0';
while(x/1000 > 0)
{
if(!stk.isFull())
stk.push(x%1000);
x = x/1000;
}
if(!stk.isFull())
stk.push(x);
while(!stk.isEmpty())
{
sprintf(s, "%s%d,", s, stk.pop());
}
int i = 0;
while(s[i++]!='\0');
*(s+i-2) = '$';
printf(s);
}
int main()
{
_dollar(12800900);
return 0;
}
Another simpler way to do it.
char* to_dollar(int x)
{
static char s[100];
int size = sizeof(s)/sizeof(s[0]);
int index = 0;
int index2 = 0;
s[size-1] = '\0';
s[size-2] = '$';
while(x>0)
{
if(index2==3)
{
s[size-index-3] = ',';
index2 = 0;
}
else
{
s[size-index-3] = x%10+'0';
x = x/10;
index2++;
}
index++;
}
return s+size-index-2;
}
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
void reverseString(char* str)
{
char tmp;
int len = strlen(str);
for(int i = 0; i <= len/2; ++i)
{
tmp = str[i];
str[i] = str[len-1-i];
str[len-1-i] = tmp;
}
}
char* to_dollar(int a)
{
stringstream in;
in << a;
char* str = const_cast<char*>(in.str().c_str());
int len = strlen(str);
char* ret_str = new char[1+len+len/3+1];
int count = 0;
int j = 1;
for(int i = len-1; i >= 0; --i)
{
ret_str[count] = str[i];
if(j % 3 == 0)
ret_str[++count] = ',';
count++;
j++;
}
ret_str[count] = '$';
reverseString(ret_str);
ret_str[count+1] = '\0';
return ret_str;
}
int main()
{
//00201
int i = 10200;
char* str = to_dollar(i);
cout << str << endl;
return 0;
}
string to_dollar(int amount){
stringstream ss;
ss << amount;
string result;
ss >> result;
int count = (result.size()%3 ==0)? result.size()/3-1: result.size()/3 ;
int counter = 0;
for( int i = 1; i <= count; i++){
result.insert(result.size()-i*3 - counter, ",");
counter++;
}
result.insert(0,"$");
return result;
}
std::string to_dollar(const int amt) {
std::deque<std::string> csRslt;
int rVal = amt;
while (rVal != 0) {
int cVal = rVal % 1000;
rVal /= 1000;
std::stringstream ss;
ss << cVal;
csRslt.push_front(ss.str());
}
std::string csAmnt("$");
for (size_t i = 0; i < csRslt.size(); ++i) {
csAmnt += csRslt[i];
if (i != (csRslt.size()-1)) csAmnt += ",";
}
return csAmnt;
}
this is the solution not only for the intergers but also for double values (using cents)
}
- wsoethe March 25, 2010Result:
$1,234,567.99